简体   繁体   English

从侧边栏反应在内容组件中打开反应引导模式

[英]React opening react-bootstrap modal in content component from sidebar

I was wondering what would be best way to open a react-bootstrap modal from sidebar that is in another nested component?我想知道从另一个嵌套组件中的侧边栏打开 react-bootstrap 模式的最佳方法是什么?

To clarify a bit I have a modal an it is rendered in the component which lists items and it is used for edit them when you click one of the items.为了澄清一点,我有一个模式,它在列出项目的组件中呈现,当您单击其中一个项目时,它用于编辑它们。 What i would like to do is when user clicks on the button in the sidebar that it opens that modal to add a item to the list (change from edit to add).我想做的是当用户单击侧边栏中的按钮时,它会打开该模式以将项目添加到列表中(从编辑更改为添加)。

At first i was thinking to move the modal to parent of the sidebar but the problem is i would have to pass props 4 times to get to the list, and that could be confusing to someone who will later edit this code.起初我想将模式移动到侧边栏的父级,但问题是我必须传递道具 4 次才能到达列表,这可能会让稍后编辑此代码的人感到困惑。

Next i taught about the context and sure that could work but to make a global state just for that it loses its purpose.接下来我讲授了上下文,并确保它可以工作,但是为了失去它的目的而制作一个全局 state 。

I taught about using refs but i am not sure on how to implement them.我教过如何使用 refs,但我不确定如何实现它们。

And last thing what i taught about was just render the modal as a new component inside the sidebar but it wont change much since i want to update the list once user has added the item and i would be in the same spot.我教的最后一件事就是将模态渲染为侧边栏中的新组件,但它不会有太大变化,因为一旦用户添加了项目,我想更新列表并且我会在同一个位置。

And i would like to avoid directly accessing DOM with id (if possible), because i would like to find a "react way" of doing this.而且我想避免使用 id 直接访问 DOM(如果可能的话),因为我想找到一种“反应方式”来做到这一点。

For example (just to visualize not the actual code)例如(只是为了可视化而不是实际代码)

<Root>
  <Component1>
    <Component2>
      <SideBar>
       <Button onClick={setShowModal(true)}> <!-- click here -->
      </SideBar>
    </Component2>
    <Component3>
     <Component4>
       <Modal show={showModal}/> <!-- open modal here -->
     </Component4>
  </Component3>
 </Component1>
</Root>

I would just like a hint on how to approach this and what would be the best way to do this.我只是想知道如何解决这个问题以及最好的方法是什么。

The most "React" way would be to store the state on the component that is the first common ancestor of the button and the modal.最“反应”的方式是将 state 存储在作为按钮和模式的第一个共同祖先的组件上。 The downside to this approach, as you mentioned, is that you will have to pass this state down.正如您所提到的,这种方法的缺点是您必须将这个 state 向下传递。 I think this would still be a clean approach.我认为这仍然是一种干净的方法。

Another thing you could consider for this case is an event emitter .对于这种情况,您可以考虑的另一件事是事件发射器 You have the Modal listen to an event to change its "open" state:您让 Modal 监听事件以更改其“打开”state:

import EventEmitter from 'EventEmitter';

const eventEmitter = new EventEmitter();

export default function Modal() {
  const [open, setOpen] = React.useState(false);

  const handleToggleModal = () => {
    setOpen(!open); 
  }

  React.useEffect(() => {
    eventEmitter.addListener('toggle-modal', handleToggleModal)

    return () => {
      eventEmitter.removeListener('toggle-modal', handleToggleModal);
    }
  }, [])

 // ...

}

In the component that opens/closes the modal, you will then have to emit the corresponding event:在打开/关闭模式的组件中,您将不得不发出相应的事件:

eventEmitter.emit('toggle-modal'); 

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

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