我有兩個功能:
const SelectCheckBox = (obj, isChecked) => {
if (isChecked) {
result = parseFloat(obj.value);
} else {
result -= parseFloat(obj.value);
}
};
const SelectAllRadioButton = (objList) => {
setSelectAll(true);
result = 0;
for (const property in objList) {
result = parseFloat(objList[property].value);
}
};
{props.List?.map((item, index) => renderRow(item, index))}
const renderRow = (obj, index) => (
<Field
checked={selectAll}
component={checkboxComponent}
name={index}
onChange={(isChecked) => SelectCheckBox({ index, obj }, isChecked)}
/>
)
當我單擊時,SelectAllRadioButton所有復選框都被選中。當我單擊 SelectCheckBox 時,應取消選中我單擊的復選框。但是,我不能這樣做,因為checked={selectAll}。我該如何處理
uj5u.com熱心網友回復:
您需要為每個單獨的復選框設定一個布林值。您對所有這些都使用相同的值。
您可以使用陣列作為狀態,其中每個元素代表具有相同索引的復選框的選中狀態。
const [checks, setChecks] = useState(props.List ? props.List.map(item => false) : [])
const toggleChecked = (idx) => {
setChecks(checks.map((item, index) => {
index === idx ? !item : item
}))
};
const SelectAllRadioButton = (objList) => {
setChecks(checks.map((item) => true))
};
{props.List?.map((item, index) => renderRow(item, index))}
const renderRow = (obj, index) => (
<Field
checked={checks[index]}
component={checkboxComponent}
name={index}
onChange={(isChecked) => toggleChecked(index)}
/>)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/443812.html
上一篇:React-Native:進行api呼叫并將資料傳遞給子項顯示為“未定義”
下一篇:ReactPropTypes-或
