del = x = thissetState heroListsplicex1 thisstateheroListsplicex1 render let heroList name = thisstate; return h2英雄联盟h2 ul heroListmapitem inde
修改删除功能,使得点击英雄名称也可以删除。
del = (x) => { const { heroList } = this.state; heroList.splice(x, 1); this.setState({ heroList }); }
render() { const { heroList, name } = this.state; return ( <>
英雄联盟
-
{heroList.map((item, index) => (
- <span onClick={() => this.del(index)}>{item} <button onClick={() => this.del(index)}>删除英雄 ))}
<input type="text" onInput={this.fn} value={name} />
<button onClick={this.add}>点击添加新英雄</button>
</>
);
原文地址: https://www.cveoy.top/t/topic/eSp6 著作权归作者所有。请勿转载和采集!