React with Hooks: When do re-renders happen? - javascript

When will a React component using hooks re-render?
Let's assume the component:
Manages state with useState
Receives props from its parent
Will a re-render happen directly after the following events, and only at those points in time?
Component receives new props
state is updated
Related Question
Let's assume the component has several useState expressions and a user interaction causes multiple states to update.
Will the component re-render multiple times, once per state value that changed, or will it batch these related changes into one single re-render?

A component will re-render in the following cases considering it doesn't implement shouldComponentUpdate for class component, or is using React.memo for function
Component receives new props
state is updated
Context value is updated(if the component listenes to context change using useContext)
Parent component re-renders due to any of the above reasons
Let's assume the component has several useState expressions and a user
interaction causes multiple states to update.
Will the component re-render multiple times, once per state value that
changed, or will it batch these related changes into one single
re-render?
useState doesn't merge the updated values with previous ones but performs batching like setState and hence if multiple state updates are done together, one render take place.

Found this nice article on this topic. I'll add the summary in case someone needs a quick read.
What causes a render in react?
State modification
Component re-renders when its state is manipulated, simple as that. So, when you modify the state of your component it tends to re-renders with the new state value.
Passing Props
If the parent component has triggered a rerender all the child component will rerender too, generally you would only want the specific child components to rerender only if the component is consuming some props or the passed props are modified but that isn’t the case, it does not matter if the props are consumed, modified or not, the child components will re-render if the parent component has triggered a render.
Using the Context API
When modifying the state which is also the value passed to the context, the whole child components tree would get rerendered. Again doesn’t matter if the child component is consuming the context data or not, it will still rerender. The rerenders depend on the value passed to the provider but still, all the whole components tree would get rerendered.
Passing down references
In case of an object, array and, function comparison is based on references i.e. the exact memory location so, their comparison even though they seem equal yields false, in React Object.is method is used to compare the different values.
Example:
{}==={} //false
[]===[] //false
{a: 5}==={a: 5} //false
[1,2,3]===[1,2,3] //false
(when comparing the previous state to the new state, it returns false because they do not have the same reference. Only the value is the same)

Related

React/Redux - When to use state and props / when to change props

My current understanding of mapStateToProps() is to map the currently set Redux store to any prop you want set within a component. I also know that props are variables that are passed to the component andstate` within the component is just a concurrent group of variables that are being utilized.
What is confusing me is that I am seeing on many sites/docs that you should never change the props.. Surely mapStateToProps() is physically changing the components props and goes against this ethos?
Here's a scenario, say we are wanting to show a loading symbol while some backend process is being run, I can create a dispatch that will set IS_LOADING= True, if I wanted to apply this value to show the loader in my component I would apply this to a prop loading in the component, however when the component first loads wouldn't it error on running mapStateToProps() as state.loading does not exist yet?
What is confusing me is that I am seeing on many sites/docs that you should never change the props
That means you should not change props from inside component. The idea is that parent creates props for the child. If props change components rerenders.
however when the component first loads wouldn't it error on running mapStateToProps() as state.loading does not exist yet?
It should not happen if you configure your store properly. Store is created on the top level, so when your child component initializes store is already present. So you have to configure your initial state properly. E. g. set initial isLoading value to false.

List of events, that cause React component to re-render

I want to know, what are events, that cause React component to re-render.
I couldn't find full list anywhere, it'd be great if someone writes the list of events, that cause React component to re-render.
I always find the following reference website helpful.
http://reactcheatsheet.com/
Filter by lifecycle events, and you can see the places where setState will trigger a rerender.
__
Update: you now have to filter by "misc"
React component is re-rendered when setState() is called or when props change. You can also force re-render with forceUpdate()
https://reactjs.org/docs/react-component.html#forceupdate
By default, when your component's state or props change, your
component will re-render. However, if these change implicitly (eg:
data deep within an object changes without changing the object itself)
or if your render() method depends on some other data, you can tell
React that it needs to re-run render() by calling forceUpdate().

Possible to only re-render one prop of component in react-native, not whole component?

I know that it is possible to re-render only one component (instead of whole DOM) using shouldComponentUpdate(), but is it possible to only re-render one PROP of one component? In one instance in my app I do not want the entire component to update, but only a specific prop that is being changed.
You probably can't do that because that's not how it's supposed to work. The component is updated in order to reflect all changes made to its state and props. The best way to get around this would be to just not update the props being passed by the parent component.
Alternatively, you could also use the state to hold the values you don't want to be updated, and set these values in the constructor of the component.

React props not updating

I have a parent class component that has a function in componentWillMount that loops through a list of strings and creates an element for each one and finally setState on this.state.map. Each child element receives a prop which is a state in parent called this.state[element].
The reason I put these elements into this.state.map was because I can change things in (some) upper components without having to re-render the array each time (which is painfully slow) and I didn't want to use shouldComponentUpdate. Plus in the future I can just quickly change elements by toggling different map states.
The problem is, when the parent this.state[element] (that's passed to child as props in the initial componentWillMount) changes, it doesn't update the props for the child. Parent state does change though.
Is this a legit way to do this?
I'm sorry I didn't provide code sample. It's just such a mess at the moment.
Only ReactDOM.render (and serverside rendering etc.), setState, or forceUpdate, will cause your children to re-render. If you are just mutating the state object, React is unaware of this, and will not re-render. Anyways, mutating the state not through setState is not valid, and you should try to keep your state as immutable as possible. In order for you to see the changes in your children, you need to use setState to update this.state[element].
From the React docs:
Notes:
NEVER mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.

reactjs forceUpdating another component

I have been using redux .. i am changing state tree using using redux actions ...
sometimes updating the state does not refresh the component .. i have to force update by this.forceUpdate() .. why is this happening? Under what condition does this happen.. usually updating state tree using an action automatically refreshes the component but sometimes it does not.. another question is that what if the state change needs to re-render a different component ?? how to i force update other components from another component ??
Are you sure what your return new object from your reducers?
If your return the same object (only mutated) redux does not understand what data has changed.
If you need rerender component when data change that that component should be dependent on these data. forceUpdate is a really bad practice.

Categories

Resources