Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react how to update state array

const initialState = [
    { name: "foo", counter: 0 },
    { name: "far", counter: 0 },
    { name: "faz", counter: 0 }
  ];

const [state, setState] = useState(initialState);

const clickButton = () => {
	// 1. Make a shallow copy of the array
	let temp_state = [...state];
	
	// 2. Make a shallow copy of the element you want to mutate
	let temp_element = { ...temp_state[0] };
	
	// 3. Update the property you're interested in
	temp_element.counter = temp_element.counter+1;
	
	// 4. Put it back into our array. N.B. we *are* mutating the array here, but that's why we made a copy first
	temp_state[0] = temp_element;
	
	// 5. Set the state to our new copy
	setState( temp_state );
}
Comment

react native update state array of objects

let markers = [ ...this.state.markers ];
markers[index] = {...markers[index], key: value};
this.setState({ markers });
Comment

change object in array in state

const toggleDone = (id) => {
  console.log(id);

  // loop over the todos list and find the provided id.
  let updatedList = state.todos.map(item => 
    {
      if (item.id == id){
        return {...item, done: !item.done}; //gets everything that was already in item, and updates "done"
      }
      return item; // else return unmodified item 
    });

  setState({todos: updatedList}); // set state to new object with updated list
}
Comment

update object in array state react

const handleAdd = (todo) => {
  const newTodos = [...todos];
  newTodos.push(todo);
  setTodos(newTodos);
}
Comment

react native update state array of objects

let newMarkers = markers.map(el => (
      el.name==='name'? {...el, key: value}: el
))
this.setState({ markers });
Comment

PREVIOUS NEXT
Code Example
Javascript :: mdn trim 
Javascript :: textcontent javascript 
Javascript :: run file with nodemon 
Javascript :: how to check two different length array values are equal in javascript 
Javascript :: angular9 spy 
Javascript :: axios js 
Javascript :: d3.js 
Javascript :: every in javascript 
Javascript :: sum float values in jquery 
Javascript :: konva line thickness 
Javascript :: filter method javascript 
Javascript :: 100 day javascript challenge 
Javascript :: react input radio button 
Javascript :: check if a string matches a regex javascript 
Javascript :: how to use msg.send instead of msg.reply discord.js javascript 
Javascript :: create a customer in stripe node.js 
Javascript :: js 1 minute sleep 
Javascript :: push json data into a list of objects in flutter 
Javascript :: useroutes how to use 
Javascript :: @input in angular 
Javascript :: convert a signed 64.64 bit fixed point number online 
Javascript :: uncaughtException javascript 
Javascript :: mongoose find in array 
Javascript :: spawn prop with custom model 
Javascript :: how to implement certain actions after setstate in react hooks 
Javascript :: js code to accept all linkedin requests 
Javascript :: how to get gmt time in javascript 
Javascript :: https://jsonplaceholder.typicode.com/albums/1 
Javascript :: how to add background to kaboom js 
Javascript :: npm install say unmet dependencies 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =