دورات حياة مكونات ReactJS بكل سهولة
المراحل هي
- التهيئة - بدء رحلة المكون الخاص بك
- تصاعد - ولادة المكون الخاص بك
- تحديث - نمو المكون الخاص بك
- Unmount - موت المكون الخاص بك
- Initialization — Starting the journey of your component
- Mounting — Birth of your component
- Update — Growth of your component
- Unmount — Death of your component
1. التهيئة
1. Initialization
This is the phase in which the component is going to start its journey. The developer has to define the props and initial state of the component. This is
usually done inside the constructor method.
2. التركيب
- componentWillMount (): كما يوحي الاسم بوضوح ، يتم استدعاء هذه الطريقة قبل تثبيت مكون على DOM مباشرة ، أي يتم استدعاء هذه الوظيفة مرة واحدة قبل تنفيذ وظيفة التقديم () لأول مرة. بعد هذه الطريقة ، يتم تثبيت المكون.
- componentDidMount (): على غرار الطريقة السابقة ، تُستدعى هذه الطريقة بعد تثبيت المكوِّن على DOM ومرة واحدة فقط في دورة الحياة. قبل تنفيذ هذه الطريقة ، يُطلق على طريقة العرض (أي يمكننا الوصول إلى DOM). يمكننا إجراء مكالمات API وتحديث الحالة باستجابة API.
2. Mounting
Mounting is the phase of the component lifecycle when the initialization of the component is completed and the React component mounts on the DOM (i.e., is created and inserted into the DOM) and rendered for the first time
on the webpage. It has 2 predefined functions:-
componentDidMount(): Similarly to the previous one this method is called after the component gets mounted on the DOM and only once in a lifecycle. Before the execution of this method, the render method is called (i.e., we can access the DOM). We can make API calls and update the state with the API response.
3. التحديث
الطرق المتوفرة في هذه المرحلة هي:
3. Updating
Updation is the phase where the states and props of a component are updated followed by some user events such as clicking, pressing a key on a keyboard etc. This is where a component’s state changes and hence, re-rendering takes place.




The methods that are available in this phase are



shouldComponentUpdate: This will determine if the component will be updated or not. The Function fulfills the requirement by letting React know whether the component’s output will be affected by every state or props update or not. It is invoked before rendering an already mounted component when new props or states are being received. If returned false then the subsequent steps of rendering will not be carried out. This method is not called for the initial render or when forceUpdate() is used. The Function takes the new Props and new State as the arguments and returns whether to re-render or not by returning a true or false value. By default, it is set to true. This method only exists as a performance optimization.
componentWillUpdate: is called just before rendering i.e. the function gets invoked once before the render() function is executed after the updation of State or Props.
componentDidUpdate: is called just after rendering.This is also a good place to do network requests as long as you compare the current props to previous props (e.g. a network request may not be necessary if the props have not changed).componentDidUpdate() will not be invoked if shouldComponentUpdate() returns false.
4. فك التركيب
اقرا المزيد
مقارنة بين React و Vue.js وAngularJS
In English
4. Unmounting
This is the final phase of the lifecycle of the component that is the phase of unmounting the component from the DOM. The following function is the sole member of this phase.
componentWillUnmount(): This function is invoked before the component is finally unmounted and destroyed from the DOM.
You should not call setState() in componentWillUnmount() because the component will never be re-rendered. Once a component instance is unmounted, it will never be mounted agai


تعليقات
إرسال تعليق