Skip to main content
improved formatting
Source Link
rozsazoltan
  • 19.5k
  • 8
  • 54
  • 158

Easiest, if you are using ES6.

initialArray = [1, 2, 3];

newArray = [ ...initialArray, 4 ];4]; // --> [1,2,3,4]

New array will be [1,2,3,4]

to update your state in React

this.setState({
  arrayvar: [...this.state.arrayvar, newelement]
});

Learn more about array destructuring

Easiest, if you are using ES6.

initialArray = [1, 2, 3];

newArray = [ ...initialArray, 4 ]; // --> [1,2,3,4]

New array will be [1,2,3,4]

to update your state in React

this.setState({
  arrayvar:[...this.state.arrayvar, newelement]
});

Learn more about array destructuring

Easiest, if you are using ES6.

initialArray = [1, 2, 3];

newArray = [...initialArray, 4]; // --> [1,2,3,4]

New array will be [1,2,3,4]

to update your state in React

this.setState({
  arrayvar: [...this.state.arrayvar, newelement]
});

Learn more about array destructuring

deleted 14 characters in body
Source Link
David Hellsing
  • 108.7k
  • 44
  • 181
  • 214

Easiest, if you are using ES6.

initialArray = [1, 2, 3];

newArray = [ ...initialArray, 4 ]; // --> [1,2,3,4]

New array will be [1,2,3,4]

to update your state in React

this.setState({
         arrayvar:[...this.state.arrayvar, newelement]
       });

Learn more about array destructuring

Easiest, if you are using ES6.

initialArray = [1, 2, 3];

newArray = [ ...initialArray, 4 ]; // --> [1,2,3,4]

New array will be [1,2,3,4]

to update your state in React

this.setState({
         arrayvar:[...this.state.arrayvar, newelement]
       });

Learn more about array destructuring

Easiest, if you are using ES6.

initialArray = [1, 2, 3];

newArray = [ ...initialArray, 4 ]; // --> [1,2,3,4]

New array will be [1,2,3,4]

to update your state in React

this.setState({
  arrayvar:[...this.state.arrayvar, newelement]
});

Learn more about array destructuring

added 28 characters in body
Source Link
StateLess
  • 5.4k
  • 3
  • 22
  • 30

Easiest, if you are using ES6.

initialArray = [1, 2, 3];

newArray = [ ...initialArray, 4 ]; // --> [1,2,3,4]

New array will be [1,2,3,4]

to update your state in React

this.setState({
         arrayvar:[...this.state.arrayvar, newelement]
       });

Learn more about array destructuring

Easiest, if you are using ES6.

initialArray = [1, 2, 3];

newArray = [ ...initialArray, 4 ]; // --> [1,2,3,4]

New array will be [1,2,3,4]

to update your state in React

this.setState({arrayvar:[...this.state.arrayvar, newelement]});

Learn more about array destructuring

Easiest, if you are using ES6.

initialArray = [1, 2, 3];

newArray = [ ...initialArray, 4 ]; // --> [1,2,3,4]

New array will be [1,2,3,4]

to update your state in React

this.setState({
         arrayvar:[...this.state.arrayvar, newelement]
       });

Learn more about array destructuring

added 71 characters in body
Source Link
StateLess
  • 5.4k
  • 3
  • 22
  • 30
Loading
added 2 characters in body
Source Link
StateLess
  • 5.4k
  • 3
  • 22
  • 30
Loading
added 18 characters in body
Source Link
StateLess
  • 5.4k
  • 3
  • 22
  • 30
Loading
deleted 12 characters in body
Source Link
StateLess
  • 5.4k
  • 3
  • 22
  • 30
Loading
added 121 characters in body
Source Link
StateLess
  • 5.4k
  • 3
  • 22
  • 30
Loading
Source Link
StateLess
  • 5.4k
  • 3
  • 22
  • 30
Loading