I did the same thing that indicated the most recent response to this post to show or hide an element in the response.js file to show and hide my elements.
But it only works the first time a button is pressed. If I enter a different value in my form and resubmit it, the data will be the same as before. He never goes to the "Results" a second time ahead. What am I doing wrong?
This is the code from the link above:
var Search = React.createClass({
getInitialState: function() {
return { showResults: false };
},
onClick: function() {
this.setState({ showResults: true });
},
render: function() {
return (
<div>
<input type="submit" value="Search" onClick={this.onClick} />
{ this.state.showResults ? <Results /> : null }
</div>
);
}
});
var Results = React.createClass({
render: function() {
return (
<div id="results" className="search-results">
Some Results
</div>
);
}
});
ReactDOM.render(<Search />, document.getElementById('container'));
EDIT new code:
module.exports = React.createClass({
getInitialState: function() {
console.log("in getInitialState");
return { showResults: false };
},
onClick: function(e) {
e.preventDefault();
data = null;
console.log("in onClick");
ticId = ReactDOM.findDOMNode(this.refs.ticketid).value;
if (this.state.showResults == true)
{
var newdata = "";
$.ajax({
url: 'http://localhost:x/' + 'tickets',
type: 'GET',
dataType: 'json',
async: false,
headers: {
"Authorization": "Basic " + btoa(u + ":" + p)
},
data: {ticketid: ticId},
success: function(result) {
newdata = result.results;
}
});
if(newdata)
{
console.log(newdata);
}
this.state.data = newdata;
}
else
{
this.setState({ showResults: true });
}
},
render: function() {
return (
<div className='someclass'>
<form className="someForm" onSubmit={this.onClick}>
<input type="text" placeholder = "Enter ticket Id" ref="ticketid" />
<input type="submit" value="Find!!" />
{ this.state.showResults ? <Results /> : null }
</form>
</div>
);
}
});
source
share