C
C
cyberlain2016-01-20 10:02:20
React
cyberlain, 2016-01-20 10:02:20

How, when choosing an option from a select, render the second unique select associated with it?

For example, I chose the region of Russia in the select, a list of districts of this region appears, then a list of settlements. Are there examples online? )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
123 123, 2016-01-20
@cyberlain

const states = ['', 'Moscowskaya'];
const cities = {
  'Moscowskaya': ['Moscow']
};

const Selects = React.createClass({
  getInitialState() {
    return { state: '' };
  },
  _onStateChange(event) {
    this.setState({ state: event.target.value });
  },
  render() {
    return (
      <div>
        <select onChange={this._onStateChange}>{states.map((state) => <option key={state} value={state}>{state}</option>)}</select>
      {this.state.state ? <select>{cities[this.state.state].map((city) => <option key={city} value={city}>{city}</option>)}</select> : null}
      </div>
    );
  }
});

jsbin.com/towebuxeji/edit?js ,output

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question