I'm looking for a way to dynamically render components based on the selected dropdown value. I'm using the ternary expression to check if it's equal to a static value now, but I'm wondering if it's possible, (If so, some direction would be much appreciated), to dynamically update the expression's value as well as the component rendered. Here is the code as it stands with being able to 'statically' render two components based on a static value.
componentDidMount() {
axios.get('http://localhost:3000/api/groups')
.then(response => {
this.setState({ groups: response.data });
})
.catch(function (error) {
console.log(error);
});
}
groupList() {
if (this.state.groups instanceof Array) {
return this.state.groups.map(function (groupsList, i) {
return <ListGroupName group={groupsList} key={i} />;
})
}
}
handleChange = e => {
this.setState({ selectedGroup: e.target.value });
}
/*** select element with options pulled from db via axios ***/
<select
value={this.state.selectedGroup}
className="browser-default"
onChange={this.handleChange}
>
<option disabled >Choose User Group</option>
{this.groupList()}
</select>
/* Is it possible to have "B&W Energy Services" be a dynamic value that changes as well
as have the component be dynamically rendered if it matches the selected value? */
{this.state.selectedGroup === "B&W Energy Services" ? <BwEnergyServices /> : <Viachem />}