Reputation: 815
I'm not sure what I am doing wrong here. ReactJS is pretty new to me, so I need some help into the right direction.
What I want to achieve is when the span is clicked on a single list item, it should remove the list item. I created a removeHandler function on the parent component, and tried passing the function down via props.
The code compiles fine, but when I try to run it in the browser I get this in my console:
Uncaught TypeError: Cannot read property 'props' of undefined
at createTasks (file:///C:/Users/Laurens/Desktop/ReactJS/output/myCode.js:9556:26)
at Array.map (native)
at Object.render (file:///C:/Users/Laurens/Desktop/ReactJS/output/myCode.js:9562:33)
at file:///C:/Users/Laurens/Desktop/ReactJS/output/myCode.js:13530:21
at measureLifeCyclePerf (file:///C:/Users/Laurens/Desktop/ReactJS/output/myCode.js:12809:12)
at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (file:///C:/Users/Laurens/Desktop/ReactJS/output/myCode.js:13529:25)
at ReactCompositeComponentWrapper._renderValidatedComponent (file:///C:/Users/Laurens/Desktop/ReactJS/output/myCode.js:13556:32)
at ReactCompositeComponentWrapper._updateRenderedComponent (file:///C:/Users/Laurens/Desktop/ReactJS/output/myCode.js:13480:36)
at ReactCompositeComponentWrapper._performComponentUpdate (file:///C:/Users/Laurens/Desktop/ReactJS/output/myCode.js:13458:10)
at ReactCompositeComponentWrapper.updateComponent (file:///C:/Users/Laurens/Desktop/ReactJS/output/myCode.js:13379:12)
Not sure what I am doing wrong, I have these two components:
var TodoList = React.createClass({
getInitialState: function() {
return {
items: [] // De initial state (state wanneer het component gemount wordt) voor items wordt aangemaakt en is per definitie leeg.
};
},
addItem: function(e) {
var itemArray = this.state.items;
if(this._inputElement.value){ // Controleer of er een value is gepost. Een lege taak willen we immers niet toevoegen aan de todoItems.
itemArray.push( // Zoja, push dan een een nieuw item aan de itemArray.
{
text: this._inputElement.value, // De text van het volgende item is gelijk aan wat er in het textfield is ingevoerd.
key: Date.now() // De key moet uniek zijn, dus gebruiken we Date.now() voor een zoe goed als unieke key.
}
);
}
this.setState({
items: itemArray // Vervolgens update de state items met itemArray.
});
this._inputElement.value = "";// Het textfield wordt weer leeg gemaakt.
e.preventDefault();
},
handleRemove: function(id){
const remainder = this.state.data.filter((item) => {
if(item.key !== id) return item;
});
this.setState({
items: remainder
});
},
render: function() {
return (
<div className="todoListMain">
<div className="header">
<h3>ToDo lijst webapp</h3>
<form onSubmit={this.addItem}>
<input ref={(a) => this._inputElement = a} placeholder="enter task">
</input>
<button type="submit">add</button>
</form>
</div>
<TodoItems remove={this.handleRemove} entries={this.state.items}/>
</div>
);
}
});
And the following:
var TodoItems = React.createClass({
render: function() {
var todoEntries = this.props.entries;
function createTasks(item) {
return <li key={item.key}>{item.text}<span onClick={this.props.handleRemove(item.key)}>X</span></li>
}
var listItems = todoEntries.map(createTasks);
return (
<ul className="theList">
{listItems}
</ul>
);
}
});
Upvotes: 1
Views: 586
Reputation: 66
var TodoItems = React.createClass({
render: function() {
var todoEntries = this.props.entries;
function createTasks(item) {
return <li key={item.key}>{item.text}<span onClick={() => this.props.remove(item.key)}>X</span></li>;
}
var listItems = todoEntries.map(createTasks,this);
return (
<ul className="theList">
{listItems}
</ul>
);
}
});
Upvotes: 1
Reputation: 1159
change <span onClick={this.props.handleRemove(item.key)
to <span onClick={this.props.remove(item.key)
the props name is remove not handleRemove
and do todoEntries.map(createTasks, this);
also this.props.remove.bind(this, item.key)
Upvotes: 0
Reputation: 1944
What David said is true, but you need to do an additional thing, so I'm copying David's answer along with the fix:
change <span onClick={this.props.handleRemove(item.key)
to <span onClick={this.props.remove.bind(this)(item.key)
the props name is remove not handleRemove
Upvotes: 0