I am learning react and struggling to get a second dropdown to populate based on which option is clicked from first dropdown.
I have included my code below.
I think the issue is where I try to set this.state.selected = param.tableName. I don't think that will work, but I'm not sure what to use instead.
I need an onClick event to change the this.state.selected variable to the option that is selected I think, and then check when tableName === selected. I will also include my JSON below so the context is more clear.
import React from 'react';
import axios from 'axios';
class Search extends React.Component {
constructor(props) {
super(props)
this.state = {
params: [],
columnParams: [],
selected: ''
}
}
componentWillMount() {
axios.get('http://localhost:3010/api/schema')
.then( response => {
this.setState({params: response.data})
})
}
render() {
return (
<div className="Search">
<select>{this.state.params.map((param, i) =>
<option key={i} onClick={this.setState(this.state.selected ={param.tableName})}>
{param.tableName}
</option>)}
</select>
<select>
{
this.state.params
.filter(({tableName}) => tableName === selected)
.map(({columns}) => columns.map((col) =><option>{col}</option>))}
[
{
"tableName": "customer",
"columns": [
"customerid",
"title",
"prefix",
"firstname",
"lastname",
"suffix",
"phone",
"email"
]
},
{
"tableName": "product",
"columns": [
"productid",
"name",
"color",
"price",
"productadjective",
"productmaterial"
]
},