React 구성 요소 수명 주기
구성 요소의 수명 주기를 이해하는 것은 사용자 친화적인 응용 프로그램을 구축하는 데 있어 기본입니다.
반응 구성 요소에는 초기화, 마운트, 업데이트 및 마운트 해제의 4가지 주요 단계가 있습니다.
INITIALIZATION
- The set-up phase where we set the state. It is usually done within the constructor method.MOUNTING
- The process of placing elements onto the DOM.
- This will be the first time an element will be rendered on a page.UPDATING
- occurs whenever there is a change to a component's STATE or PROPS.UNMOUNTING
- As the name implies, this is when a component is REMOVED from the DOM
React 구성 요소에는 호출 시 수명 주기의 특정 단계에서 호출되는 메서드가 있습니다. 이들 중 대부분은 선택 사항이며 대부분 자명하지만 중요한 기능을 제공합니다.
하나씩 분해해 보겠습니다.
초기화
- constructor() - (optional**) Similar as using constructor in JS classes, constructor is used the same way in React passing in 'props' as an argument. Within the constructor scope is the invocation of super(props). Calling super will allow us access to variables from the parent class while passing in props will allow us to pass data from one component to another [3]
**constructor() 호출은 기술적으로 선택 사항이지만 일반적으로 다음 두 가지 이유로 호출하려고 합니다.
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
};
}
설치
- getDerivedStateFromProps() - (optional) called before render. Here we can set the state based on the initial props.[1]
- componentWillMount() - (optional) called before render. After this method, the component will get mounted.
- render() - (required) this method is what actually puts our HTML on the page. Once it is run, you will be able to see your component. When changes are made to the props/state this method will be invoked again to re-render the page.
- componentDidMount - (optional) called after render(). can be used to change the state after it has successfully rendered onto the page.
업데이트 중
- getDerivedStateFromProps() - (optional) same functionality when used in mounting. see above.
- shouldComponentUpdate() - (optional) returns a boolean. if set to false, the component will not update when a state/prop is changed. If true or not called, it will allow updates to occur. Essentially, this method makes a re-render optional.
- componentWillUpdate() - accepts two arguments: next props and next state. It is called once after shouldComponentUpdate and before a re-render.
- getSnapshotBeforeUpdate - (optional) accepts two arguments: previous props, and previous state. It allows the access to any props and stats before the component was updated. If using this method, you are required to have componentDidUpdate() called as well, otherwise there will be bugs on your app.
- componentDidUpdate() - (optional) called after a component is updated in the DOM [3].
마운트 해제
구성 요소를 마운트 해제하려고 할 때 실행되는 메서드는 componentWillUnmount()뿐입니다.
- componentWillUnmount() - (required) immediately after removing component from DOM, this will invoke. This denotes the end of a component's lifecycle and is required when properly trying to remove a component from the app.
[1] https://www.w3schools.com/react/react_lifecycle.asp
[2] https://www.javatpoint.com/react-constructor#:~:text=The%20constructor%20is%20a%20method,before%20the%20component%20is%20mounted .
[3] https://www.geeksforgeeks.org/whats-the-difference-between-super-and-superprops-in-react/
[4] https://www.freecodecamp.org/news/how-to-understand-a-components-lifecycle-methods-in-reactjs-e1a609840630/
[5] https://reactjs.org/docs/react-component.html#:~:text=If%20you%20don't%20initialize,props)%20before%20any%20other%20statement .
Reference
이 문제에 관하여(React 구성 요소 수명 주기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/vkton115/react-component-lifecycle-13c9텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)