I am beginner in reactJS
, I am trying to make a webapp in which there are 3 buttons , initially the content of first button should be visible then after clicking any other button I want to hide the content of previous and show new content corresponds to the new button pressed.
Like ,say, I have 3
buttons A,B and C
on the screen in a row. Having content as "Button A is pressed"
, "Button B is pressed"
and "Button C is pressed"
Now initially content of A which is "Button A is pressed" should be shown below the buttons.
Now when I press any other button the content of B
should be visible which is "Button B is pressed"
and A's
content should be hidden. Now this happens again and again like, when I press A
again then "Button A is pressed"
should be visible and content of B
should be hidden. Same goes for C
.
What I searched and I didn't understood how to apply it as they are just toggling or displaying and not fulfilling what I want to do?
https://reactjs.org/docs/faq-functions.html
What I have implemented?
var ASearch = React.createClass({
getInitialState: function() {
return { showResultsA: true };
},
onClick: function() {
this.setState({ showResultsA: true, showResultsB: false, showResultsC: false });
},
render: function() {
return (
<div>
<Button type="submit" placeholder="Button A" onClick={this.onClick} />
{ this.state.showResultsA ? <AResults /> : null }
</div>
);
}
});
var BSearch = React.createClass({
getInitialState: function() {
return { showResultsB: false };
},
onClick: function() {
this.setState({ showResultsA: false, showResultsB: true, showResultsC: false });
},
render: function() {
return (
<div>
<Button type="submit" placeholder="Button B" onClick={this.onClick} />
{ this.state.showResultsB ? <BResults /> : null }
</div>
);
}
});
var CSearch = React.createClass({
getInitialState: function() {
return { showResultsC: false };
},
onClick: function() {
this.setState({ showResultsA: false, showResultsB: false, showResultsC: true });
},
render: function() {
return (
<div>
<Button type="submit" placeholder="Button C" onClick={this.onClick} />
{ this.state.showResultsC ? <CResults /> : null }
</div>
);
}
});
var AResults = React.createClass({
render: function() {
return (
<div id="Aresults" className="search-results">
Button A is Pressed
</div>
);
}
});
var BResults = React.createClass({
render: function() {
return (
<div id="Bresults" className="search-results">
Button B is Pressed
</div>
);
}
});
var CResults = React.createClass({
render: function() {
return (
<div id="Cresults" className="search-results">
Button C is pressed
</div>
);
}
});
These I have created outside my class importing React.
export default class Program extends React.Component {
render{
return( <ASearch />
<BSearch />
<CSearch />);
}