【发布时间】:2021-03-30 15:24:45
【问题描述】:
当我按下删除图标时,我希望删除该项目。但是有序列表开头的数字没有损坏。它是类组件。 removeItem 方法应该是什么?
目前没有代码,因为我尝试了很多东西,但没有任何效果,所以我最终在 console.logs 上查看发生了什么,所以我删除了它
const ProfileSettings = observer(
class ProfileSettings extends React.Component {
constructor(props) {
super(props)
this.state = {
expandIconPosition: 'left',
buttonDisable: false,
fetchedNotificationData: [],
spinning: true,
checkedA: true,
checkedB: true,
checkedC: true,
list: [],
};
}
addListItem = () => {
var test = this.state.list;
test.push({ text: "",selected:false });
this.setState({
list: test
});
}
removeItem = () => {
???????????
}
<div style={{ backgroundColor: "#ffffff", borderRadius: 16, padding: 10, minHeight: 140, width: 406, borderRadius: 8, marginTop: 10, boxShadow: "0px 1px 3px 0px rgba(0, 0, 0, 0.2), 0px 2px 1px -1px rgba(0, 0, 0, 0.12), 0 1px 0 rgba(0, 0, 0, 0.14)" }}>
{(this.state.list.length !== 0) ? this.state.list.map((row) => (
<div className="row" style={{borderRadius:4,marginLeft:5,marginRight:5,paddingLeft : 15, backgroundColor : (row.selected) ? "#1A32FF" : "#FFF" }}>
<span style={{ marginTop: 2,color:(row.selected) ? "#FFF" : "black" }}>{this.state.list.indexOf(row) + 1}.</span>
<TextField InputProps={{
style:{
color: row.selected ? 'white' : 'black'
},
onClick:()=>this.changeRowSelectedOption(row)
}}id="standard-basic" style={{ width: "50%",marginLeft:3 }}></TextField>
<IconButton className={(row.selected) ? "selectedDeleteIconclass" : "deleteIconclass" } style={{ float: "right" , marginLeft : 115,marginTop:-8}} onClick={() => this.removeItem()}>
<Delete />
</IconButton>
</div>
)) : null}
</div>
</div>
【问题讨论】:
-
test.push(不要在 React 中改变状态... -
其实我什么都不懂:(什么意思?
-
stackoverflow.com/questions/37755997/… 你正在改变状态。除非您想要错误和不直观的行为,否则不要这样做。
-
好的,我明白了。那么我该怎么做 removeItem 我该怎么做呢?
-
听起来你可能想学习一些关于 React 的教程。 React's site actually has a pretty good one.