How do I remove the item when user clicked x? pass the id to parent and use filter()? In jquery I can just use remove() and that's about it. Very new to react, need guidance.
import React from 'react';
const RenderItem = (props) => {
return(
<ul id="todo">
{props.items.map((item,i) =>
<li className='list-group-item' data-id={item.id} key={i}>{item.name}
<button className="btn btn-sm btn-primary onClick={}">X</button>
</li>
)}
</ul>
)
};
const TodoItems = React.createClass({
getInitialState() {
return {
items: [
{id:1,name:"Gym"},
{id:2,name:"Jump"},
{id:3,name:"Racing"}
],
editing: false
}
},
edit(){
this.setState({editing: true})
},
save(){
this.setState({editing: false})
},
remove(id){
//return this.items.filter((item,i) => item.id !== id)
}
render(){
return(
<RenderItem items={this.state.items} />
)
}
})
ReactDOM.render(
<TodoItems />,
document.getElementById('container')
);
<div id="container">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
This is my code on fiddle https://jsfiddle.net/3mn105sj/, I don't know why I failed to use react here.
Remove stand-alone element onclick in ReactAttach an event handler to the onClick event of the element. In the event handler, negate the value of the visibility state to remove the element from the DOM.
HTML DOM Element remove() The remove() method removes an element (or node) from the document.
In a browser, we need to regenerate the HTML views in the Document Object Model (DOM). With React, we do not need to worry about how to reflect these changes, or even manage when to take changes to the browser; React will simply react to the state changes and automatically update the DOM when needed.
Check the fiddle:
https://jsfiddle.net/zp5oqnsh/1/
const RenderItem = (props) => {
return(
<ul id="todo">
{props.items.map((item,i) =>
<li className='list-group-item' data-id={item.id} key={i}>{item.name}
<button className="btn btn-sm btn-primary" onClick={() => props.remove(item.id)}>X</button>
</li>
)}
</ul>
)
};
const TodoItems = React.createClass({
getInitialState() {
return {
items: [
{id:1,name:"Gym"},
{id:2,name:"Jump"},
{id:3,name:"Racing"}
],
editing: false
}
},
edit(){
this.setState({editing: true})
},
save(){
this.setState({editing: false})
},
remove(id){
this.setState({
items: this.state.items.filter((el) => id !== el.id)
})
//return this.items.filter((item,i) => item.id !== id)
},
render(){
return(
<RenderItem items={this.state.items} remove={this.remove}/>
)
}
})
ReactDOM.render(
<TodoItems />,
document.getElementById('container')
);
Pass the remove()
as a props and call the remove()
onClick with the id and apply filter..
Hope it helps!
you need to use react-dom for example;
import { unmountComponentAtNode } from 'react-dom';
onDismiss(node) {
unmountComponentAtNode(node);
document.body.removeChild(node);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With