I have a react component which represents a row in a table of words. It re-renders when the state changes, but it does not update onClick event handler. I have stripped down the component to the bare minimum and it still has the issue. When I click the "Edit" link, the console prints "onEdit" and the state appears to look correct in the browser, but then when I click "Revert", the console prints "onEdit" a second time. Why is the onClick event not being updated to use the _onRevert function after the state changes? Why it is still hanging on to the old event handler from the first render pass? Thank you.
var React = require('React/addons');
var WordListItem = React.createClass({
getInitialState: function(){
return {
isEditing: false,
english: this.props.word.english
};
},
_onEdit: function(e) {
e.preventDefault();
console.log('onEdit');
this.setState({isEditing: true});
},
_onRevert: function(e){
e.preventDefault();
console.log('onRevert');
this.setState({isEditing: false});
},
render: function() {
if(this.state.isEditing) {
return (
<div>
<span>Editing: {this.state.english} </span>
<a href="#" onClick={this._onRevert}>Revert</a>
</div>
)
} else {
return (
<div>
<span>Not Editing: {this.state.english} </span>
<a href="#" onClick={this._onEdit}>Edit</a>
</div>
)
}
}
})
module.exports = WordListItem;
UPDATE BELOW
So it turns out that it works if I paste this component into my main app.jsx file, it works, but not if I include it using require js. Which is weird, because browserify is working since I can still include other files and compose nested react components, it just that it the events don't get updated on some elements. Maybe at this point I should ask a new question, which is more focused.
var React = require('react/addons');
//var materialize = require('materialize-css');
var word = {id: 4671810846, english: "water", persian: "آب", phonetic: "aab", tags: ["noun","food","drink"]};
var WordListItem = require('./components/wordList/WordListItem.jsx');
// var WordListItem = React.createClass({
// getInitialState: function(){
// return {
// isEditing: false,
// english: this.props.word.english
// };
// },
// _onEdit: function(e) {
// e.preventDefault();
// console.log('onEdit');
// this.setState({isEditing: true});
// },
// _onRevert: function(e){
// e.preventDefault();
// console.log('onRevert');
// this.setState({isEditing: false});
// },
// render: function() {
// if(this.state.isEditing) {
// return (
// <div>
// <span>Editing: {this.state.english} </span>
// <a href="#" onClick={this._onRevert}>Revert</a>
// </div>
// )
// } else {
//
// return (
// <div>
// <span>Not Editing: {this.state.english} </span>
// <a href="#" onClick={this._onEdit}>Edit</a>
// </div>
// )
// }
//
// }
// })
React.render(<WordListItem word={word}/>, document.getElementById('app'));
thisand click Edit and then click Revert, both _onEdit and _onRevert handlers log the samethisobject, and the state of isEditing is set to true inside this object. However, this.state.isEditing first prints false, and then it prints true.console.logs of "onEdit" and "onRevert", as expected.