再開!7 Lifecycle
マウント/アンマウント
インストールは、DOMを作成してWebブラウザに表示することを意味し、逆も同様です.
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}
componentDidMount() {
}
componentWillUnmount() {
}
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
構成部品クラスに特別なメソッド(ex.componentDidMount)を宣言して、構成部品のマウントとアンロード時に特定のコードを実行できます.Recap !
When
React then calls the Clock component’s render() method. This is how React learns what should be displayed on the screen. React then updates the DOM to match the Clock’s render output.
When the Clock output is inserted in the DOM, React calls the componentDidMount() lifecycle method. Inside it, the Clock component asks the browser to set up a timer to call the component’s tick() method once a second.
Every second the browser calls the tick() method. Inside it, the Clock component schedules a UI update by calling setState() with an object containing the current time. Thanks to the setState() call, React knows the state has changed, and calls the render() method again to learn what should be on the screen. This time, this.state.date in the render() method will be different, and so the render output will include the updated time. React updates the DOM accordingly.
If the Clock component is ever removed from the DOM, React calls the componentWillUnmount() lifecycle method so the timer is stopped.
1. constructor
2. render
3. componentDidMount
4. tick
5. render
6. tick
7. render
Reference
この問題について(再開!7 Lifecycle), 我々は、より多くの情報をここで見つけました https://velog.io/@vlrtpfdkxm/다시-React-7-Lifecycleテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol