Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we partially update a state in react?

I was wondering what is the best way to partially update a state of a component in React/React-Native. Other than the fact that I can make a function which takes the current state and creates a new state and merges the new {key:value} and the previous state. For example:

{
      dataStream:[//having data here], 
      formData: {
         'first_name': 'Richard',
         'last_name' : 'Barbieri',
      } 
}

I want to update last_name to another value. When I call this.setState(formData:{{'last_name':newValue}}), it resets the formData dictionary to just last name: new Value. Is there a way to this efficiently?

like image 614
Debojit Kaushik Avatar asked Mar 20 '17 08:03

Debojit Kaushik


People also ask

How do you update part of state in React?

To update our state, we use this. setState() and pass in an object. This object will get merged with the current state. When the state has been updated, our component re-renders automatically.

Can we directly modify state in React?

React will then look at the virtual DOM, it also has a copy of the old virtual DOM, that is why we shouldn't update the state directly, so we can have two different object references in memory, we have the old virtual DOM as well as the new virtual DOM.

How do I update nested state React?

To update nested properties in a state object in React: Pass a function to setState to get access to the current state object. Use the spread syntax (...) to create a shallow copy of the object and the nested properties. Override the properties you need to update.

How does React handle multiple state updates?

React may batch multiple setState calls into a single update for performance. Because props and state may be updated asynchronously, you should not rely on their values for calculating the next state.

How to update the state of a React component?

React useState () hook manages the state in functional React components. In class components this.state holds the state, and you invoke the special method this.setState () to update the state. Mostly using state in React is straightforward. However, there’s an important nuance to be aware of when updating the state.

Why is it so hard to update a nested state in react?

The pain of updating a nested state stems from the fundamental architectural decision of the React to embrace immutability. Immutability comes with plenty of great benefits, such as predictability and performance, so the trade-off is worth it. There are two main ways to deal with the problem of updating a deeply nested state.

What are the types of State in react?

There are four main types of state you need to properly manage in your React apps: Local (UI) state – Local state is data we manage in one or another component. Local state is most often managed in React using the useState hook.

What is the difference between local and global state in react?

Local state is most often managed in React using the useState hook. For example, local state would be needed to show or hide a modal component or to track values for a form component, such as form submission, when the form is disabled and the values of a form’s inputs. Global (UI) state – Global state is data we manage across multiple components.


2 Answers

I think there are two things you could try:

  1. Spread operator

liks so

this.setState({
    formData: {
        ...this.state.formData, 
        "last_name" : newValue 
    } 
});

or

  1. Take current state's first_name and reapply it:

like so

this.setState({ 
    formData: { 
        "first_name": this.state.formData.first_name, 
        "last_name" : newValue 
    } 
})

I'm not too sure about the first one, but I think the second one should work.

like image 153
A. L Avatar answered Oct 08 '22 21:10

A. L


What happens is normal because you reassign the whole forData.

If you want to add something to the existing form data do something like that (there are plenty of other solutions ^^)

this.setState({
    formData: Object.assign(this.state.formData, { 'last_name': newValue } 
})
like image 4
Ji aSH Avatar answered Oct 08 '22 22:10

Ji aSH