javascript - Using state and button to remove an element from a list -
i have list created so:
return (this.state.limit).fill().map((_,index) => { return ( <div key={`${index}`}> item </div> ) )
how can create button let's me remove specific div element reduce state limit?
in react, have bind data. in example, items should represented underlying data. here's basic example showing this:
class example extends react.component { constructor() { super(); this.state = { data: ['item 1', 'item 2', 'item 3', 'item 4', 'item 5'], }; } removeitem(item) { let data = this.state.data.filter((_, i) => != item); this.setstate({ data }); } render() { return ( <div> {this.state.data.map((item, i) => <div key={i} onclick={() => this.removeitem(i)}>{item}</div>)} </div> ); } } reactdom.render(<example/>, document.getelementbyid('view'));
<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> <div id="view"></div>
Comments
Post a Comment