カウンタボタンを作成しようとしていますが、できません。私はバインディングとは何かを知っていますが、私は解決策を見つけることができません。私は.bind(これ)を使用してみましたが、動作しません。メソッドをバインドする方法
class Button extends React.Component{
render(){
return(
<button onClick={this.props.localHandleClick}>+1</button>
)
}
}
class Result extends React.Component{
render(){
return (
<div>{this.props.localCounter}</div>
)
}
}
class Main extends React.Component{
constructor(props){
super(props);
this.state={
counter:0
}
}
clickHandler(){
this.setState({counter:
this.state.counter+1});
}
render(){
return(
<div>
<Button localHandleClick={this.handleClick}/>
<Result localCounter={this.state.counter} />
</div>
)
}
}
ReactDOM.render(
<Main />,
document.getElementById("app")
)
<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>
<div id="app"></div>
は、あなたのコンストラクタにこれを追加 - 'this.clickHandler = this.clickHandler.bind(this) '。 –
React.jsの[OnClickイベントバインディング](https://stackoverflow.com/questions/27397266/onclick-event-binding-in-react-js)の可能な複製 –
これを試しました。これは動作しません。 –