はなぜ、この単純なコンポーネントの作業を行いますが、私は私が使用している場合である通常の関数は矢印機能リアクト(キャッチされない例外TypeErrorを:プロパティを読み取ることができません。)
に矢印機能を変更しない場合は、人に反応:
function() {
this.setState({
count: this.state.count + 1
})
}, 1000
代わりに
() => this.setState({
count: this.state.count + 1
}), 1000
は
生成しますUncaught TypeError: Cannot read property 'count' of undefined at eval (eval at transform.run (browser.js:3193), :27:29)
の作業コード:
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {count: 0};
}
componentDidMount() {
setInterval(
() => this.setState({
count: this.state.count + 1
}),
1000
);
}
render() {
return (
<div>{this.state.count}</div>
);
}
}
ReactDOM.render(
<Counter />,
document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<body>
<div id="root"></div>
</body>
矢印機能+ this:期待していないもの – Weedoze