Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react state add to array


// Save search term state to React Hooks with spread operator and wrapper function

// Using .concat(), no wrapper function (not recommended)
setSearches(searches.concat(query))

// Using .concat(), wrapper function (recommended)
setSearches(searches => searches.concat(query))

// Spread operator, no wrapper function (not recommended)
setSearches([...searches, query])

// Spread operator, wrapper function (recommended)
setSearches(searches => [...searches, query])
Comment

add new array at the back of react state

this.setState(prevState => ({
  myArray: [...prevState.myArray, "new value"]
}))
Comment

how to add array data on state react

this.setState({ myArray: [...this.state.myArray, 'new value'] }) //simple value
this.setState({ myArray: [...this.state.myArray, ...[1,2,3] ] }) //another array
Comment

add new array at the back of react state

this.setState(prevState => ({
  myArray: ["new value", ...prevState.myArray]
}))
Comment

add new array at the back of react state

this.setState(prevState => ({
  myArray: [...prevState.myArray, {"name": "object"}]
}))
Comment

add object in array state react

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

add object in array state react


this.setState(prevState => ({
  arrayvar: [...prevState.arrayvar, newelement]
}))

Comment

add new array at the back of react state

this.setState(prevState => ({
  myArray: [ {"name": "object"}, ...prevState.myArray]
}))
Comment

add array of object to state react


var joined = this.state.myArray.concat('new value');
this.setState({ myArray: joined })

Comment

PREVIOUS NEXT
Code Example
Javascript :: add class with jquery 
Javascript :: javascript function uppercase to lowercase 
Javascript :: hashset in javascript 
Javascript :: how to use js console log 
Javascript :: javascript string to number 
Javascript :: react keys 
Javascript :: react app 
Javascript :: lodash sort json 
Javascript :: navigator.geolocation.getCurrentPosition(console.log,console.log) undefined 
Javascript :: js add begin array 
Javascript :: how to draw a horizontal line in javascript 
Javascript :: javascript push object into array with variable key 
Javascript :: jquery load 
Javascript :: window on resize 
Javascript :: add two numbers in jquery 
Javascript :: js log stack trace 
Javascript :: import syntax node 
Javascript :: how to change css variable in javascript 
Javascript :: React 18 to 17 
Javascript :: jquery select all checkboxes except disabled 
Javascript :: loop over an array 
Javascript :: convert a string to array in javascript 
Javascript :: how to use platform.select 
Javascript :: display image on button click javascript 
Javascript :: variable used in a function can be used in another function js 
Javascript :: jquery select input with empty value 
Javascript :: how to export a function in nodejs 
Javascript :: TypeError: JSON.stringify(...).then is not a function 
Javascript :: delete last character from string js 
Javascript :: How to make remove buttoon on table using js DOM 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =