JavaScript
2
Вклад в тег
removeCount = (idx) => (/*сюда будет передан event, но он вам типа не нужен*/) => {
/* тело с this, он будет autobinded */
}
class App extends React.Component {
//constructor(){
// super();
// this.state ={
// count: [1,2,3,4,5,6,7,8,9]
// }
// this.removeCount = this.removeCount.bind(this)
//}
this.state = {
count: [1,2,3,4,5,6,7,8,9]
};
/*...*/
}
class App extends React.Component {
constructor(){
super();
this.state ={
count: [1,2,3,4,5,6,7,8,9]
}
this.removeCount = this.removeCount.bind(this)
}
removeCount(idx) {
return function() {
let newItems = this.state.count;
newItems.splice(idx, 1);
this.setState({count: newItems});
// this.setState({count: this.state.count.slice(/*чо нада*/)});
// все что выше в методе тогда не нужно
}
}
render() {
const list = this.state.count.map(function(index, i){
return (
<div
key={i}
onClick={this.removeCount(i)}>
<p>{index}</p>
</div>
);
})
return (
<div className="row">
{list}
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('container'));
class App extends React.Component {
state ={
count: [1,2,3,4,5,6,7,8,9]
}
removeCount = (idx) => () => {
this.setState({count: this.state.count.slice(idx, 1)});
}
render() {
return (
<div className="row">
{this.state.count.map(i => (
<div
key={i}
onClick={this.removeCount(i)}>
<p>{i}</p>
</div>
))}
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('container'));