如果我在React Native中有一個列表,並且每個列表項都有一個複選框,我怎樣才能得到所有這些複選框的狀態?我有一個複選框列表,我如何得到它們的狀態
現在,我使用這個
{
this.props.data.allLogs.map((r) => {
return (
<ListItem
key={r.id}
title={r.logDate}
onPress={() => this.toggle(r.id)}
subtitle={
<CheckBox
title={`start: ${r.startedAtOdometerReading} -> end: ${r.endedAtOdometerReading}`}
checked={this.state.checkboxes[r.id]}
onPress={() => this.toggle(r.id)}
onIconPress={() => this.toggle(r.id)}
/>
}
/>
);
})
}
toggle(id) {
let checkboxes = this.state.checkboxes;
checkboxes[id] = !this.state.checkboxes[id];
this.setState({checkboxes});
}
和它的作品(複選框打開和關閉沒有問題),但是當我退出this.state.checkboxes我得到一個空數組...
很難給出太多指導,不會對您設置的詳細信息,但你可能需要用戶註銷時組件將卸載和狀態都將丟失 –