D
D
danilr2019-05-27 09:46:02
React
danilr, 2019-05-27 09:46:02

How to properly make a select multilpe handler?

class FlavorForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: 'b'};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState((state,props)=>{
      value: event.target.value});
  }

  handleSubmit(event) {
    alert('Your favorite flavor is: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick your favorite flavor:
          <select multiple={true} value={['a', 'c']} onChange={this.handleChange}>
            <option value="a">Grapefruit</option>
            <option value="b">Lime</option>
            <option value="c">Coconut</option>
            <option value="d">Mango</option>
          </select>
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

ReactDOM.render(
  <FlavorForm />,
  document.getElementById('root')
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Alexandrovich, 2019-05-27
@danilr

https://codepen.io/gaearon/pen/JbbEzX?editors=0010
https://reactjs.org/docs/forms.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question