Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To transmit an array using setState in JSX, you can simply pass the array as a value to the state object.

Here's an example:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      myArray: ["one", "two", "three"]
    };
  }

  render() {
    return (
      <div>
        {/* display the state array */}
        {this.state.myArray.map(item => <p>{item}</p>)}

        {/* update the state array */}
        <button onClick={() => this.setState({ myArray: ["four", "five", "six"] })}>
          Update myArray
        </button>
      </div>
    );
  }
}

In this example, the initial state includes an array (myArray) with three items. The array is then displayed using the map function within the render method.

When the button is clicked, the state is updated using the setState method, which includes a new array of items as the value for myArray.