我正在尝试获取无状态子组件的高度,以便能够在父类中使用它的高度,但是我得到了以下错误:Invariant Violation: Stateless function components cannot have refs.
简化代码
父类
class App extends React.Component {
componentDidMount() {
console.log(this.header);
}
render() {
return (
<Child ref={component => this.header = component} />
)
}
}孩子
const Child = () => (
<header ref="header">
Some text
</header>
)有办法这样做吗?
这里有一个指向有错误的代码的链接。
更新:
实际代码/上下文
所以我现在有一个标题组件,它看起来是这样的:
export const Header = ({dateDetailsButton, title, logo, backButton, signUpButton, inboxButton, header}) => (
<header header className="flex flex-row tc pa3 bb b--light-gray relative min-h-35 max-h-35">
{signUpButton ? <Link to="/edit-profile-contact" href="#" className="flex z-2"><AddUser /></Link> : null }
{backButton ? <BackButton className="flex z-2" /> : null }
<h1 className={logo ? "w-100 tc dark-gray lh-solid f4 fw5 tk-reklame-script lh-solid ma0" : "w-100 tc dark-gray lh-solid f4 fw4 absolute left-0 right-0 top-0 bottom-0 maa h1-5 z-0"}>{title}</h1>
{dateDetailsButton ? <Link to="/date-details" className="absolute link dark-gray right-1 top-0 bottom-0 maa h1-5 z-2">Details</Link> : null }
{inboxButton ? <Link to="/inbox" href="#" className="flex mla z-2"><SpeechBubble /></Link> : null}
</header>
)在某些情况下,我希望向这个标题添加逻辑以使其动画化(例如,在主页上,当用户滚动时,我会将标题动画化,当他们滚动超过某个点时(如果您愿意的话,这是一个粘稠的标题)。
我以前这样做的方法是只为具有特定功能的头(如上面所述)拥有一个单独的类,而另一个没有。但是为了保持代码的干净度,我已经将头分离出来作为它自己的功能无状态组件,并将其封装在给它提供粘性头功能的Class中。
下面是这门课的内容:
export default class FeedHeader extends React.Component {
constructor(props) {
super(props);
this.handleScroll = this.handleScroll.bind(this);
this.state = {
scrolledPastHeader: null,
from: 0,
to: 1,
}
}
componentDidMount() {
window.addEventListener('scroll', this.handleScroll);
this.setState({navHeight: this.navHeight.offsetHeight});
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll(e) {
const { navHeight, scrolledPastHeader } = this.state;
const wy = document.body.scrollTop;
if (wy < navHeight) this.setState({scrolledPastHeader: null})
else if (wy > navHeight && wy < 100) {
this.setState({scrolledPastHeader: false})
}
else this.setState({scrolledPastHeader: true})
}
getMotionProps() {
const { scrolledPastHeader } = this.state;
return scrolledPastHeader === false ?
{
style: {
value: this.state.from
}
}
: {
style: {
value: spring(this.state.to)
}
}
}
render() {
const { brand, blurb } = this.props;
const { scrolledPastHeader } = this.state;
return (
<Motion {...this.getMotionProps()}>
{({value}) => {
return (
<Header ref="child" title={this.props.title} backButton={false} signUpButton inboxButton logo/>
);
}}
</Motion>
)
}
}因此,这就是这个特殊问题的背景--我希望这能让事情变得更清楚一些。
很抱歉,没有上下文,我以为答案会比看起来更直接!
发布于 2017-08-06 14:32:24
好的,首先,感谢大家的回复--真的很感谢!
我开始按照Win推荐的方式实现react-waypoints,尽管很明显,我需要修改代码才能使其工作,所以我想我需要最后一次搜索,尝试使用refs找到另一个解决方案。
我正在寻找的答案实际上非常简单,来自于下面的Github发布线程 (由孟彭纳编写)。
解决方案
因为React不允许引用无状态功能组件,所以您可以在包装器中包装功能组件,同时为包装器提供自己的ref,如下所示:
render() {
return (
<div ref={el => {this.childWrap = el;}}>
<Child />
</div>
)
}然后,可以通过针对包装器访问组件的高度:
componentDidMount() {
if(this.childWrap && this.childWrap.firstChild) {
let childHeight = this.childWrap.offsetHeight;
console.log(childHeight);
}
}虽然这可能不是最优雅的解决方案,但它确实解决了我目前的问题。
这是一位书呆子供参考。
发布于 2017-08-05 20:09:47
这是一种使用“反应路径点”的解决方案。您可以在这里查看:https://codesandbox.io/s/qp3ly687
我在onEnter和onLeave中添加了一个选项,这样它就不会疯狂地更新状态,这样我们就可以在滚动时优化性能,并且不会锁定应用程序。同样,这是一个非常粗略的想法,您可以做什么,并有许多选项,以改进的实现。
===上一版本
这是通过保持无状态组件来解决问题的一种方法。由于没有进一步解释上下文,这将给您一个机会来了解如何获取位于无状态组件中的子组件的高度。
class App extends React.Component {
componentDidMount() {
console.log(this.child.offsetHeight);
}
render() {
return (
<div>
<Wrapper>
<div
ref={child => this.child = child}
style={{height: 500}}>
Some text!!
</div>
</Wrapper>
</div>
)
}
}
const Wrapper = ({children}) => {
return (
<div>
<header>Header</header>
<div>{children}</div>
<footer>Footer</footer>
</div>
)
}
ReactDOM.render(
<App />,
document.getElementById("main")
);
https://stackoverflow.com/questions/45525713
复制相似问题