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

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 object to array setstate


 this.setState( prevState => ({
     userFavorites: [...prevState.userFavourites,  {id: 3, title: 'C'}]
 }));

Comment

push values to state array class react

this.setState(prevState => ({
  arrayvar: [...prevState.arrayvar, newelement]
}))
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 object to array setstate

To push to the beginning of the array do it this way

   this.setState( prevState => ({
     userFavorites: [{id: 3, title: 'C'}, ...prevState.userFavourites]
  }));
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 :: express.static 
Javascript :: how to remove duplicate array object in javascript 
Javascript :: js throw custom error 
Javascript :: get time from date 
Javascript :: javascript remove final newline from string 
Javascript :: validate password javascript 
Javascript :: remove element from array in usestate 
Javascript :: random string from array javascript 
Javascript :: js two array combining with id 
Javascript :: sort string 2d array in javascript 
Javascript :: reverse a word javascript 
Javascript :: find whitespace in string js 
Javascript :: js input type range on hover 
Javascript :: how to navigate programatically in class component react router v6 
Javascript :: react open pdf in new tab 
Javascript :: alphabet only in jquery 
Javascript :: jquery add multiple classes 
Javascript :: create react app cmd 
Javascript :: javascript integer to string 
Javascript :: js convert array of array to array 
Javascript :: comments in json 
Javascript :: how to get last item in array javascript 
Javascript :: generate guard angular 
Javascript :: promise.allsettled polyfill node 
Javascript :: convert cookies to json javascript 
Javascript :: libraries like html-react-parser 
Javascript :: MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string. 
Javascript :: jsx render array 
Javascript :: js for each item in array 
Javascript :: how to get multiple checkbox value in jquery 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =