Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 无法从antd模态组件参数函数onOk和onCancel更新复选框_Reactjs_Antd_Use State - Fatal编程技术网

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重现这个示例呢?