1 回答

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
因?yàn)槟闼衖tem的狀態(tài)都是用QRScreening來判斷的,解決方法:
在onQRScreening傳入index,將點(diǎn)擊的index存入QRScreening中,通過判斷QRScreening中是否存在當(dāng)前點(diǎn)擊對(duì)象的index來改變開關(guān)的狀態(tài)。
大致代碼如下(我用的react版本是15.4.2):
state = { QRScreening: [] }
onQRScreening = (index) => {
let QRScreening = this.state.QRScreening
// 判斷是否點(diǎn)擊過,點(diǎn)擊過則刪除,未點(diǎn)擊過則添加
QRScreening.indexOf(index) === -1 ? QRScreening.push(index) : QRScreening.splice(QRScreening.indexOf(index), 1)
this.setState({
QRScreening
})
}
render() {
return (
<div>
{[1, 2, 3].map((m, index) => {
let flag = this.state.QRScreening.indexOf(index) === -1
return (
<div key={index} onClick={() => this.onQRScreening(index)}>
{flag ? '關(guān)閉' : '開啟'}
</div>
)
})}
</div>
)
}
添加回答
舉報(bào)