Reactjs 无法从antd模态组件参数函数onOk和onCancel更新复选框
我有这样一个模态组件:Reactjs 无法从antd模态组件参数函数onOk和onCancel更新复选框,reactjs,antd,use-state,Reactjs,Antd,Use State,我有这样一个模态组件: import { Modal } from 'antd'; const customModal = () => { const [isCustomModalVisible, setIsCustomModalVisible] = useState(false); let [customCheckBoxValue, setCustomCheckBoxValue] = useState(false); useEffect(() => {
import { Modal } from 'antd';
const customModal = () => {
const [isCustomModalVisible, setIsCustomModalVisible] = useState(false);
let [customCheckBoxValue, setCustomCheckBoxValue] = useState(false);
useEffect(() => {
alert('customCheckBoxValue' + customCheckBoxValue);
}, [customCheckBoxValue]);
const onClick = (e) => {
setIsCustomModalVisible(true);
// setCustomCheckBoxValue(!customCheckBoxValue);
}
const onCancel = () =>{
setIsCustomModalVisible(false);
setCustomCheckBoxValue(false);
}
const onOk = () => {
setCustomCheckBoxValue(true);
};
return (
<span>
<span id='customCheckBoxValue'>{customCheckBoxValue}</span>
<input id="customCheckbox" type="checkbox" onClick={onClick} checked={customCheckBoxValue} />
<Modal
title={'Custom modal'}
visible={isCustomModalVisible}
okText="Yes"
onOk={onOk}
cancelText="No"
onCancel={onCancel}
maskClosable={false}>
<span> This is custom modal </span>
</Modal>
</span>
);
}
从“antd”导入{Modal};
常量customModal=()=>{
const[iscustomodalvisible,setiscustomodalvisible]=使用状态(false);
让[customCheckBoxValue,setCustomCheckBoxValue]=useState(false);
useffect(()=>{
警报(“customCheckBoxValue”+customCheckBoxValue);
},[customCheckBoxValue]);
const onClick=(e)=>{
setiscustomodalvisible(true);
//setCustomCheckBoxValue(!customCheckBoxValue);
}
const onCancel=()=>{
setiscustomodalvisible(false);
setCustomCheckBoxValue(假);
}
const onOk=()=>{
setCustomCheckBoxValue(真);
};
返回(
{customCheckBoxValue}
这是定制模式
);
}
如我们所见,customCheckbox
的值分别在onCancel
和onOk
函数中被取消选中(false)和被选中/true
每当setCustomCheckBoxValue
更改customCheckBoxValue
的值时,useEffect
会显示警报。问题在于,#customCheckbox
复选框既没有被选中/取消选中/更新,也没有被span.#customCheckBoxValue
顺便说一句,在网上重现这个错误。为了确保,您希望在“失败的尝试”中发生什么?单击复选框时,是否应选中该复选框?您的输入是受控组件,您不会更改其值。你甚至会因此受到警告。如果antd只与它们的组件相关,为什么不尝试用antd重现这个示例呢?