英文:
React JS - Modal only appearing once
问题
我正在使用React创建一个登录模态框,它会在按钮点击时打开。问题是,它只会出现一次。
以下是模态框的代码:
import { Modal } from "components/Modal";
import React, { useState, useEffect } from "react";
export default function SignInModal(props: any) {
const { showSignInModal } = props;
const [isOpen, setIsOpen] = useState(showSignInModal);
useEffect(() => {
setIsOpen(showSignInModal);
}, [showSignInModal]);
return (
<div>
<Modal
isOpen={isOpen}
onDismiss={() => {
setIsOpen(false);
}}
title=""
>
<div className="text-white">Hello World</div>
</Modal>
</div>
);
}
以下是我的index.js文件的代码:
const [showSignInModal, setShowSignInModal] = useState(false);
function handleOpenConnect() {
setShowSignInModal(true);
}
return (
<>
<Button onClick={handleOpenConnect}>
Sign In
</Button>
<SignInModal showSignInModal={showSignInModal} />
</>
)
当我再次点击按钮时,模态框不会出现。我希望每次点击按钮时都能出现。
英文:
I am creating a Sign in modal using react which opens on a button click. The problem is that only appears once.
Here's the code for the modal
import { Modal } from "components/Modal";
import React, { useState, useEffect } from "react";
export default function SignInModal(props: any) {
const { showSignInModal } = props;
const [isOpen, setIsOpen] = useState(showSignInModal);
useEffect(() => {
setIsOpen(showSignInModal);
}, [showSignInModal]);
return (
<div>
<Modal
isOpen={isOpen}
onDismiss={() => {
setIsOpen(false);
}}
title=""
>
<div className="text-white">Hello World</div>
</Modal>
</div>
);
}
Here's the code for my index.js file
const [showSignInModal, setShowSignInModal] = useState(false);
function handleOpenConnect() {
setShowSignInModal(true);
}
return (
<>
<Button onClick={handleOpenConnect} >
Sign In
</Button>
<SignInModal showSignInModal={showSignInModal} />
</>
)
When I press the button again, it does not appear. I want it to appear each time the button is clicked.
答案1
得分: 1
模态组件创建了自己的本地状态副本,并更新该本地副本。因此,对父组件状态的未来更新不会改变模态组件中的任何内容。
不要重复状态。
如果模态应该由父组件控制,它就不需要自己的内部状态。让它依赖于提供的值:
export default function SignInModal(props: any) {
const { showSignInModal, setShowSignInModal } = props;
return (
<div>
<Modal
isOpen={showSignInModal}
onDismiss={() => {
setShowSignInModal(false);
}}
title=""
>
<div className="text-white">Hello World</div>
</Modal>
</div>
);
}
并传入状态和设置器:
<SignInModal showSignInModal={showSignInModal} setShowSignInModal={setShowSignInModal} />
英文:
The modal component is creating its own local copy of state and updating that local copy. So future updates to the parent's state don't change anything in that modal component.
Don't duplicate state.
If the modal should be controlled by the parent, it doesn't need its own internal state. Let it rely on the value provided:
export default function SignInModal(props: any) {
const { showSignInModal, setShowSignInModal } = props;
return (
<div>
<Modal
isOpen={showSignInModal}
onDismiss={() => {
setShowSignInModal(false);
}}
title=""
>
<div className="text-white">Hello World</div>
</Modal>
</div>
);
}
And pass in both the state and the setter:
<SignInModal showSignInModal={showSignInModal} setShowSignInModal={setShowSignInModal} />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论