Javascript React引导模式显示未按预期工作
我正在尝试在我的应用程序中添加react引导模式。在一个文件中,我删除了模态并试图将参数发送到模态类 例如:Javascript React引导模式显示未按预期工作,javascript,reactjs,react-bootstrap,Javascript,Reactjs,React Bootstrap,我正在尝试在我的应用程序中添加react引导模式。在一个文件中,我删除了模态并试图将参数发送到模态类 例如: Class1 : I have this inside my render function. <MyModal openModal={true}/> Class1:我的渲染函数中有这个。 我正试图从这里发送道具,它将打开模态 在我的模态类中,我有以下代码: const MyModal = (props) => { const [open, showModa
Class1 : I have this inside my render function.
<MyModal openModal={true}/>
Class1:我的渲染函数中有这个。
我正试图从这里发送道具,它将打开模态
在我的模态类中,我有以下代码:
const MyModal = (props) => {
const [open, showModal] = useState(props.openModal);
const handleClose = () => showModal(false);
return (
<Modal show={open} onHide={handleClose} >
//rest of the modal data
</Modal>
);
};
export default MyModal;
constmymodal=(道具)=>{
const[open,showModal]=useState(props.openModal);
const handleClose=()=>showmodel(false);
返回(
//其余模态数据
);
};
导出默认MyModal;
我的道具总是显示来自类1的更新值。但是模态类没有按照更新道具进行渲染。第一次关闭模式后,它将保持关闭状态,直到我刷新屏幕
我使用模态链接作为参考
提前感谢您的帮助。当组件收到更新的道具时,您缺少更新组件的挂钩 您需要在代码中包含
useffect()
hook。它将使用更新的道具重新渲染组件。MyModal类的代码如下所示
const MyModal = (props) => {
const [open, showModal] = useState(props.openModal);
useEffect(() => {
showModal(props.openModal);
}, [props]);
const handleClose = () => showModal(false);
return (
<Modal show={open} onHide={handleClose} >
//rest of the modal data
</Modal>
);
};
export default MyModal;
constmymodal=(道具)=>{
const[open,showModal]=useState(props.openModal);
useffect(()=>{
showModal(道具openModal);
}[道具];
const handleClose=()=>showmodel(false);
返回(
//其余模态数据
);
};
导出默认MyModal;
谢谢,useEffect()对我来说非常有用。我想,需要通过钩子:)