Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS, Calling setState with same parameter

Tags:

reactjs

I have been reading the React docs and came across shouldComponentUpdate(). My understanding is that everytime setState() is called, a re-render of that component will be updated.

My question is that If the value to be updated is the SAME as the current state value, would this trigger a re-render event? or I would have to manually checks for the current value and value to be updated in shouldComponentUpdate()

like image 406
XPLOT1ON Avatar asked Dec 25 '16 15:12

XPLOT1ON


People also ask

Does setState Rerender with same value?

For new React developers, it is common to think that setState will trigger a re-render. But this is not always the case. If the value doesn't change, React will not trigger a re-render. This hook internally runs an interval every 500 milliseconds which is absolutely scary because inside we are always calling setDay .

Why is setState () in React async instead of sync?

Why setState() is async? ReactJs sets its state asynchronously because it can result in an expensive operation. Making it synchronous might leave the browser unresponsive. Asynchronous setState calls are batched to provide a better user experience and performance.

Why putting setState () in render () is not preferred?

The render() function should be pure, meaning that it does not modify a component's state. It returns the same result each time it's invoked, and it does not directly interact with the browser. In this case, avoid using setState() here.

Can setState be synchronized?

There is no guarantee of synchronous operation of calls to setState and calls may be batched for performance gains. So since setState() is asyncronous and there is no guarantee about its synchronous performance.

How to use the setState callback in react?

How to Use the setState Callback in React. To perform an action in a React component after calling setState, such as making an AJAX request or throwing an error, we use the setState callback. Here’s something extremely important to know about state in React: updating a React component’s state is asynchronous. It does not happen immediately.

How to change the state of a React component?

Whenever the state changes, React re-renders the component to the browser. Before updating the value of the state, we need to build an initial state setup. Once we are done with it, we use the setState () method to change the state object. It ensures that the component has been updated and calls for re-rendering of the component.

Is it safe to call setState () multiple times in react?

This can be safer than using an object argument where multiple calls to setState () are used, as multiple calls may be batched together by React and executed at once, and is the preferred approach when using current props to set state.

How do I update the state of a greeting in react?

This object contains the part of the state we want to update which, in this case, is the value of greeting. React takes this value and merges it into the object that needs it. It’s just like the button component asks what it should use for updating the value of greeting and setState () responds with an answer.


1 Answers

The official React documentation states:

The default behavior is to re-render on every state change...

https://reactjs.org/docs/react-component.html#shouldcomponentupdate

This means that by default, render() will be executed if any of a component's state or props values changes.

You can override this default behavior using shouldComponentUpdate(). Here's an example that only updates if a state value changes.

shouldComponentUpdate(nextProps, nextState) {     return this.state.someValue !== nextState.someValue; } 

Note: this example completely ignores props. So, any changes to props will not trigger render().

like image 184
Jyothi Babu Araja Avatar answered Sep 22 '22 09:09

Jyothi Babu Araja