简体   繁体   English

onClick 功能仅在第一次反应时有效?

[英]onClick function only works for the first time in react?

I have two seperate components namely Header and Modal.我有两个单独的组件,即 Header 和 Modal。 There is a button in Navbar that has an onClick function attached to it.导航栏中有一个按钮,附加了一个 onClick 函数。 By clicking it the state changes and hence the state is passed to modal as props which then triggers it.通过单击它,状态会发生变化,因此状态会作为道具传递给 modal,然后触发它。 The state is then set to false again.然后再次将状态设置为 false。 However if i click on the button again, the modal does not appear.但是,如果我再次单击该按钮,则不会出现模态。 I have tried many different thing even with useEffects but nothing worked.即使使用 useEffects,我也尝试了许多不同的东西,但没有任何效果。

Header code标题代码

const Header = () => {
const [modal, setModal] = useState(false)

return( 
<div>
{modal ? <ModalProduct showModal={true} /> : null}
<ul class="nav navbar-nav ml-auto">
         <li><Button variant="danger" style={{ marginTop: '8px' }} onClick={() => setModal(true)}>Add 
         new product</Button></li></ul>)
 </div>)

And for the Modal Component I have对于模态组件我有

export default function ModalProduct(props) {
const [show, setShow] = useState(props.showModal);

const handleClose = () =>
    setShow(false);

return (
    < div >
        <Modal show={show} onHide={handleClose}>
           ...
        </Modal>
    </div >
);

} }

There is something related to do with hooks, it gets true for the first time popping the modal and then changes to false but then does not become true again.有一些与钩子有关的事情,它在第一次弹出模态时变为真,然后变为假,但随后不会再次变为真。

Point the onClick to a function that will handle the switching of the state by if true->false and false->true.将 onClick 指向一个函数,该函数将通过 if true->false 和 false->true 处理状态的切换。

const Header = () => {
const [modal, setModal] = useState(false)

const triggerModel = () => {
  setModal(!modal)
}

return( 
<div>
{modal ? <ModalProduct showModal={triggerModel} /> : null}
<ul class="nav navbar-nav ml-auto">
         <li><Button variant="danger" style={{ marginTop: '8px' }} onClick={() => setModal(true)}>Add 
         new product</Button></li></ul>)
 </div>)

You are using 2 states for the same thing (showing/hiding the modal).您正在为同一件事使用 2 个状态(显示/隐藏模式)。 You only need one state for that.你只需要一个状态。 Remove the state from your ModalProduct component and just use the props from your parent component Header to handle the modal.从您的ModalProduct组件中删除状态,只需使用父组件Headerprops来处理模态。 I also refactored your code to make it more concise and readable.我还重构了您的代码,使其更加简洁和可读。

const Header = () => {
  const [showModal, setShowModal] = useState(false);

  return( 
    <div>
      <ModalProduct showModal={modal} handleClose={()=> setShowModal(false)} />
        <ul class="nav navbar-nav ml-auto">
            <li>
              <Button variant="danger" style={{ marginTop: '8px' }} onClick={()=>setShowModal(true)}>
                  Add new product
              </Button>
            </li>
        </ul>
</div>
export default function ModalProduct(props) {
  return (
    <Modal show={props.showModal} onHide={props.handleClose}>
      ...
    </Modal>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM