rerendered要素がstate.status要素に変更を適用しない理由を教えてください。ReactDOM.unmountComponentAtNode
これを行うには適切な方法を使用していますか?要素自体が削除されるべきではありませんが、プレゼンテーション用です。すでにレンダリングされたコンテナへのコンポーネントの再レンダリングは期待通りに機能しませんか?
class CustomModal extends React.Component {
constructor(props){
super(props)
this.state = {
status:this.props.status
}
this.onClick = this.onClick.bind(this);
}
render(){
return <h1 onClick={this.onClick}>Click Me!</h1>
}
onClick(){
console.log('May i log to console?:' + this.state.status)
this.setState({status:false});
// this is somewhat cumbersome?
{/* ReactDOM.unmountComponentAtNode(document.getElementById('container')) */}
// supposed to rerender the element?
// And the state.status element should be true?
ReactDOM.render(<CustomModal status={true} />,document.getElementById('container'))
}
}
ReactDOM.render(<CustomModal status={true} />,document.getElementById('container'))
<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="container">
</div>
あなたはあなたの例で達成しようとしているかを説明することはできますか?状態を変更すると、レンダリング関数を再度呼び出すように応答する必要があります。コンポーネントを手動でレンダリングしないでください。またstatus = {true}を渡しますが、status = {this.state.status}を使用しないでください。 –
私は徐々に反応するようにコードを置き換えています。この段階で私はthisを実行する必要があります。私は再レンダリングされた要素の状態が変わっていないという例を表示したいので、 'status = {true} 'にしてください –