ReactJS: What is React Lifecycle and Lifecycle Hook?

Lifecycle of Components

The react lifecycle method is used in the React class component. It helps us in creating our state and mutating them.
Each component in React has a lifecycle which you can monitor and manipulate during its three main phases.

The three phases are: Mounting, Updating, and Unmounting.

Common lifecycles State and Lifecycle ReactJS

Common lifecycles

All lifecycle State and Lifecycle ReactJS

All lifecycle

Lifecycle Hook of Components

Mounting

The mounting means to put elements into the DOM. React uses virtual DOM to put all the elements into the memory. It has four built-in methods to mount a component namely

  1. constructor() : method is called when the component is initiated and it’s the best place to initialize our state. The constructor method takes props as an argument and starts by calling super(props) before anything else.
  2. getDerivedStateFromProps(): is called right before rendering the element in our DOM. It takes props and state as an argument and returns an object with changes to the state.
  3. render(): This is the only compulsory method required by the React. This method is responsible to render our JSX to DOM
  4. componentDidMount(): The most common and widely used lifecycle method is componentDidMount. This method is called after the component is rendered. You can also use this method to call external data from the API.

Updating

This is the second phase of the React lifecycle. A component is updated when there is a change in state and props React basically has five built-in methods that are called while updating the components.

  1. getDerivedStateFromProps():
  2. shouldComponentUpdate(): This lifecycle method is used when you want your state or props to update or not. This method returns a boolean value that specifies whether rendering should be done or not. The default value is true.
  3. render()
  4. getSnapshotBeforeUpdate(): This method is called right before updating the DOM. It has access to props and state before the update. Here you can check what was the value of your props or state before its update. So let see how it works.
  5. componentDidUpdate(): is called after the component is updated in the DOM. This is the best place in updating the DOM in response to the change of props and state.

Unmounting

The final or the end of the react lifecycle is Unmounting. This is used when a component is removed from the DOM. React has only one built-in method that gets called when a component is unmounted

  1. componentWillUnmount(): If there are any cleanup actions like canceling API calls or clearing any caches in storage you can perform that in the componentWillUnmount method. You cannot use setState inside this method as the component will never be re-rendered.
Summary
  • The constructor() method is the best place to initialize our state
  • The getDerivedStateFromProps() is a rarely used lifecycle method and is the best place to set the state object based on the initial props.
  • The shouldComponentUpdate() specifies whether React should continue with the rendering or not.
  • The render() method is the most used and compulsory lifecycle method.
  • The getSnapshotBeforeUpdate() method has access to the props and state even after the update.
  • The componentDidMount() is the most common and widely used lifecycle method and is called after the component is rendered. You can also use this method to call external data from the API.
  • The componentDidUpdate() method is called after the component is updated in the DOM and is the best place in updating the DOM in response to the change of props and state.
  • The componentWillUnmount() happens just before the component unmounts and is destroyed and is used for cleanup actions like canceling API calls.

 

mounting

@mounting /'mouldiɳ/
* danh từ
- sự trèo, sự lên
- sự tăng lên
- giá, khung

@mounting
- (máy tính) lắp ráp; khung
- panel m. lắp ráp theo bảng
- rack m. lắp ráp trên giá

@mount /maunt/
* danh từ
- núi ((thường) đặt trước danh từ riêng (viết tắt) Mt)
= Mt Everest+ núi Ê-vơ-rét
* danh từ
- mép (viền quanh) bức tranh
- bìa (để) dán tranh
- khung, gọng, giá
- ngựa cưỡi
* ngoại động từ
- leo, trèo lên
=to mount a hill+ trèo lên một ngọn đồi
=to mount a ladder+ trèo thang
- cưỡi
=to mount a horse+ cưỡi ngựa
- nâng lên, cất lên, đỡ lên, kéo lên, cho cưỡi lên
- đóng khung, lắp táp, cắm vào, dựng lên, đặt, sắp đặt, dán vào, đóng vào
=to mount a diamond in platinum+ găn một viên kim cương vào miếng bạch kim
=to mount a photograph+ dán ảnh vào bìa cứng
=to mount a loom+ lắp một cái máy dệt
=to mount a gun+ đặt một khẩu súng
=to mount a play+ dựng một vở kịch
- mang, được trang bị
=the fort mounts a hundred guns+ pháo đài được trang bị một trăm khẩu súng
=quân to mount guard+ làm nhiệm vụ canh gác
=mỹ to mount an attack+ mở một cuộc tấn công
- cho nhảy (cái) vật nuôi
* nội động từ
- lên, cưỡi, trèo, leo
=to mount on the scaffolf+ lên đoạn đầu đài
=to mount on a horse+ cưỡi trên mình ngựa
- lên, bốc lên
=blush mounts to face+ mặt đỏ ửng lên
- tăng lên
=prices mount up every day+ giá cả ngày càng tăng
=the struggle of the people against depotism and oppression mounts+ cuộc đấu tranh của nhân dân chống chuyên chế và áp bức tăng lên

props

@props /props/
* danh từ số nhiều
- (từ lóng) đồ dùng sân khấu

@prop /prop/
* danh từ
-(ngôn ngữ nhà trường) (từ lóng), (viết tắt) của proposition, mệnh đề
- (hàng không) (từ lóng) (viết tắt) của propeller
- (sân khấu), (từ lóng), (viết tắt) của property, đồ dùng sân khấu (trang trí, phục trang, dàn cảnh...)
* danh từ
- cái chống, nạng chống
- (nghĩa bóng) người chống đỡ, người đứng mũi chịu sào; cột trụ
=the prop and stay of the home+ cột trụ trong gia đình
- (số nhiều) cẳng chân
* ngoại động từ
- chống, chống đỡ; đỡ lên, đỡ dựng lên
=to prop a ladder [up] against the wall+ dựng thang dựa vào tường
- (+ up) (nghĩa bóng) làm chỗ dựa cho, chống đỡ cho, đứng mũi chịu sào cho, làm cột trụ cho (một tổ chức, một gia đình...)
* nội động từ
- đứng sững lại (ngựa)

state

@state /steit/
* đại từ
- trạng thái, tình trạng
=a precarious state of health+ tình trạng sức khoẻ mong manh
=in a bad state of repair+ ở một tình trạng xấu
- địa vị xã hội
- (thông tục) sự lo lắng
=he was in quite a state about it+ anh ta rất lo ngại về vấn đề đó
- quốc gia, bang
=Southern States+ những bang ở miền nước Mỹ
- nhà nước, chính quyền
- sự phô trương sang trọng, sự trang trọng, sự trọng thể, sự huy hoàng
=to be welcomed in great state+ được đón tiếp rất trọng thể
=in state+ với tất cả nghi tiết trọng thể
- tập (trong sách chuyên đề)
- (từ cổ,nghĩa cổ) ngai, bệ
!to lie in state
- quàn
* tính từ
- (thuộc) nhà nước
=state documents+ những văn kiện của nhà nước
=state leaders+ những vị lãnh đạo nhà nước
- (thuộc) chính trị
=state prisoner+ tù chính trị
=state trial+ sự kết án của nhà nước đối với hững tội chính trị
- dành riêng cho nghi lễ; chính thức
=state carriage+ xe ngựa chỉ dùng trong các nghi lễ cổ truyền của Anh
=state call+ (thông tục) cuộc đi thăm chính thức
* ngoại động từ
- phát biểu, nói rõ, tuyên bố
=to state one's opinion+ phát biểu ý kiến của mình
- định (ngày, giờ)
- (toán học) biểu diễn bằng ký hiệu (một phương trình...)

@state
- trạng thái, chế độ
- absorbing s. (điều khiển học) trạng thái hút thu
- aperiodic s. trạng thái không tuần hoàn
- critical s. trạng thái tới hạn
- ergodic s. trạng thái egođic
- inaccessible s. (điều khiển học) trạng thái không đạt được
- initial s. (điều khiển học) trạng thái trong
- quasi-stationary s. trạng thái tựa dừng
- solid s. trạng thái rắn
- stationary s. (vật lí) trạng thái dừng
- steady s. [trạng thái, chế độ] ổn định
- terminal s. trạng thái cuối
- transinet s. trạng thái chuyển
- zero s. (điều khiển học) trạng thái không

updating

@update
* ngoại động từ
- làm cho cập nhật, hiện đại hoá
- cho ai thông tin mới nhất (về cái gì)
* danh từ
- sự cập nhật hoá (thông tin mới nhất)