我的组件值this.state
内部始终为null。这是代码:
class Sidebar extends Component {
static propTypes = {
static: PropTypes.bool
};
constructor(props) {
super(props);
this.state = {
static: false
};
}
componentWillReceiveProps() {
console.log(this.state);
}
onMouseEnter() {
console.log(this.state);
}
onMouseLeave() {
console.log(this.state);
}
render() {
return (
<nav className={s.root} onMouseEnter={this.onMouseEnter.bind(this)} onMouseLeave={this.onMouseLeave.bind(this)}>
</nav>
);
}
}
在控制台中我只看到null
鼠标进入,鼠标离开并接收新道具。
你有什么想法为什么会这样?
谢谢!
答案 0 :(得分:2)
我只是必须解决这个问题,并发现问题是您需要将方法绑定到对象。我在构造函数中做了这个,它解决了我的问题。
<!--comment line 123--><security>
<requestFiltering>
<hiddenSegments>
<add segment="NWebsecConfig" />
<add segment="Logs" />
</hiddenSegments>
</requestFiltering>
</security><!--comment line 123-->
答案 1 :(得分:-2)
如果您想设置初始状态,则需要实现getInitialState()
,而不是在构造函数中尝试this.state = { ... }
。
此外,在州上设置属性的正确方法是使用setState({ static: false })
,而不是this.state.static = false
。