React States Vs React Refs f. What? When? Why? How?
I am using react in one of my freelancing project. Due to the size of the app the things were getting complex regarding re-renders and performance. Then with time I develop the understanding of how they actually works. Refs is having its own usecases and states is having its own usecases.
React State
- About
React state is basically the data that is already being hold by a component. In simple words the inner properties or in development terms the encapsulated properties.
- Setting/Updating State
Setting state is basically changing the data that the component holds. But here two things will be coming in mind. If we change the data the how the component will be getting the updated data because only updating the state is not the ultimate goal , updating the data then viewing the updated data is the ultimate goal.
Well the concept of viewing the updating data is known as rendering. Basically the innerHTML value of the element is changed when state updated.
So if alot of states getting updated at one time which means alot of components getting rendered at one time then the user is going to have a laggy experience in the ui
React Refs
- About
React refs are nothing but regular js objects where the data persists across rerenders that means the data not lost when the component rerenders. But updating the data will not re renders the component. Also it can be use to target elements directly by keeping the element in the reference and perform actions such as .click()
State vs Refs
- Forms
- Setting a global value to context
- Already some other component is rendering the current component
Comments
Post a Comment