我的組件內部值this.state
始終爲空。下面是代碼:ReactJS this.state爲空
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
鼠標輸入,鼠標離開和接受新道具。
你有什麼想法爲什麼會發生?
謝謝!
適合我。你確定這是涉及這個問題的_only_代碼嗎? –
嘗試從您的代碼中刪除靜態propTypes ... – yuyokk
無法複製:https://jsbin.com/vurimac/edit?html,js,console,output。你的代碼在其他地方几乎肯定會發生另一個錯誤。 – rossipedia