Populate 2nd Dropdown Based On Selection From 1st Dropdown In React.js
Solution 1:
You're doing it wrong way. You cannot call onClick method on <option>
. Instead you should use onChange method on <select>
(see react docs) like this:
<selectonChange={this.handleChange} ><option>1</option><option>2</option><option>3</option></select>
Then on 'onChange' event you can set your state to the selected option. Let's understand this scenario with an example.
Suppose you have two dropdowns. One for showing companies and one for jobs corresponding to each company. Each company has its own set of jobs like this:
companies:[
{ name: 'company1', jobs: ['job1-1', 'job1-2', 'job1-3']},
{ name: 'company2', jobs: ['job2-1', 'job2-2', 'job2-3']},
{ name: 'company3', jobs: ['job3-1', 'job3-2', 'job3-3']}
]
Now you have to just set a state, lets say 'selectedCompany' and then filter the companies array by 'selectedCompany'. Then you can just get that particular company object and map your 'jobs' dropdown by iterating over jobs array inside the company object.
Here is the code snippet:https://codepen.io/madhurgarg71/pen/pRpoMw
Solution 2:
- event handlers for e.g.
onClick
must be a function this.setState
expects an object, which will be merged with the state so the part where you setselected
in your state must be<optionkey={i}onClick={() => { this.setState({selected: param.tableName}) }>{param.tableName}}}</option>
you use an undefined variable in your filter (
selected
), you have to use.filter(({tableName}) => tableName === this.state.selected)
instead
Solution 3:
Some things to look at:
The syntax in your call to setState
in your <option> onClick
event handler looks incorrect.
You also make try to reference this.state.selected
in your .filter
condition without the path to the variable (this.state
)
Suggested solution(s):
// The .setState method expects an object to be passed to it rather// than an expression that mutates the state directly// Pass it an object containing the properties you want to change,// ensure your use colon (':') rather than equals sign ('=') to set property:
<option key={i} onClick={
this.setState({
selected: param.tableName
})
</option>
// In the <select> tag,// you need to refer to the selected property in it as this.state.selected:// Don't forget the closing select tag, it's missing from your snippet
<select>
{
this.state.params
.filter(tableName => (tableName === this.state.selected))
.map(columns => columns.map(col => <option>{col}</option>))
}
</select>
Solution 4:
This issue can easily be dealt with by chaining a filter and map together on the second input, which filters it's options depending on the input of the first drop down.
<selectvalue={country}onChange={e => setCountry(e.target.value)}>
{countries.map(country => (
<optionvalue={country}>{country}</option>
))}
</select><selectvalue={school}onChange={e => setSchool(e.target.value)}>
{schools.filter(school => school.Country === country).map(school => (
<optionvalue={school.Name}>{school.Name}</option>
))}
</select>
Above we have two selects. The first selects a value, and then the options for the second will render based on this.
Post a Comment for "Populate 2nd Dropdown Based On Selection From 1st Dropdown In React.js"