Hi I have a react app where I am returning several buttons. What I am trying to do is depending on the button you click another component will show and render in the view. I've looked at conditional rendering in the docs but I am not sure how to tie this to the state and render what I expect. Below is a sample of whats in the component.
// import all the components I need;
class FormContainer extends React.Component {
constructor(props) {
super(props);
this.state = {
dropdownVisible: false,
};
this.handleClick = this.handleClick.bind(this);
}
handleClick(event) {
if (!this.state.dropdownVisible) {
document.addEventListener('click', this.handleOutsideClick, false);
} else {
document.removeEventListener('click', this.handleOutsideClick, false);
}
this.setState(prevState => ({
dropdownVisible: !prevState.dropdownVisible,
}));
}
handleOutsideClick(e) {
if (this.node.contains(e.target)) {
return;
}
this.handleClick();
}
render() {
return (
<form className="form-container">
<h2>Browse The Database</h2>
// here are the two buttons
<button className="btn btn-default" onClick={this.handleClick}>
By Fiscal Year
</button>
<button className="btn btn-default" onClick={this.handleClick}>
By Research Organization
</button>
// depending on which one is clicked either of these table containers should render with the appropriate component passed in
<div className="table-container">{this.state.dropdownVisible && <FiscalYearTable />}</div>
<div className="table-container">
{this.state.dropdownVisible && <ResearchOrganizationTable />}
</div>
</form>
);
}
}
export default FormContainer;
What is happening instead is that only one is rendering because I have not established the relationship between each button and the component I want to render. How can I establish this relationship using a conditional if or other method tied to the state?