enter image description hereI am trying to setState of two different sate(options1 ad options2) in my component with data gotten from firebase. The data in the state of the component is to be used for a dependent drop-down. I have been able to setState with the new firebase data(I know this because I see that the state of my component is the data from my database using the react developer tools). However, the data is not being rendered on the dropdown. Is there something I am doing wrong or should be doing.
class Impairement extends Component {
constructor() {
super();
this.state = {
name: "React",
selectedOption: {},
selectedOption2: {},
options1: [],
options2: []
};
}
componentDidMount() {
this.getnewData();
}
//firebase fetch
getnewData() {
var rootRef = firebase
.database()
.ref()
.child("test");
var opt = [];
rootRef.on("child_added", snapshot => {
opt.push({
value: snapshot.val().value,
label: snapshot.val().label
});
});
var rootRef = firebase
.database()
.ref()
.child("test2");
var optio = [];
rootRef.on("child_added", snapshot => {
optio.push({
label: snapshot.val().label,
link: snapshot.val().link,
value: snapshot.val().value
});
});
this.setState({
options1: opt
});
this.setState({
options2: optio
});
}
handleChange1 = selectedOption => {
this.setState({ selectedOption });
};
handleChange2 = selectedOption => {
this.setState({ selectedOption2: selectedOption });
};
render() {
const filteredOptions = this.state.options2.filter(
o => o.link === this.state.selectedOption.value
);
return (
<div>
<p>Select Domain</p>
<Select
name="form-field-name"
value={this.state.selectedOption.value}
onChange={this.handleChange1}
options={this.state.options1}
/>
<p>Then Subdomain</p>
<Select
name="form-field-name"
value={this.state.selectedOption2.value}
onChange={this.handleChange2}
options={filteredOptions}
/>
</div>
);
}
}
export default Impairement;