下記のようにbuttonをクリックしたら文字が出るようにして、
画面のどこでもいいから再度クリックすると
文字が消えるようにしたいのですが(stateを変更する)、
うまくいきません。
どうしたらよいでしょうか?
class Application extends React.Component { constructor(props) { super(props); this.state = { messageDisplay: 'none', }; } click = () => { this.setState({ messageDisplay:"block" }); } componentDidMount = () => { window.addEventListener("click",function() { this.setState({ messageDisplay:"none" }.bind(this)); }); } render() { return <div> <h1 style={ { display: this.state.messageDisplay } }>Hello</h1> <button onClick={this.click.bind(this)}>btn</button> </div>; } } /* * Render the above component into the div#app */ React.render(<Application />, document.getElementById('app'));
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/08 02:37
2018/08/08 02:47 編集
2018/08/08 02:54