简体   繁体   English

如何避免父母在反应中向孩子发送事件

[英]How to avoid parent sending event to child in react

I have the following situation:我有以下情况:

  • I have a component library with modal component.我有一个带有模态组件的组件库。 The modal already have action button implemented and when user click, it emits a click event.模态已经实现了动作按钮,当用户点击时,它会发出一个点击事件。
  • This modal can be used to render one of couple of component inside of it.此模式可用于渲染其中的一对组件。
  • I want to trigger an action related to the state of the child when user clicks on the action button当用户单击操作按钮时,我想触发与孩子状态相关的操作
  • The modal also has a skip button that will switch the component inside of it模态框还有一个跳过按钮,可以切换其中的组件

Modal graphic description模态图形说明

What i'm doing now is this: I have a component that accepts a key as a props, and from a pre-defined map, i will know the component i want to render (using React.lazy()), modal title, and action button text.我现在正在做的是:我有一个接受键作为道具的组件,并且从预定义的地图中,我将知道我要渲染的组件(使用 React.lazy()),模态标题,和操作按钮文本。 It works, but this way i have to use my child's function when the action button is clicked (for example with using ref) but i want to avoid this, since it's not the react way.它可以工作,但是这样我必须在单击操作按钮时使用我孩子的功能(例如使用 ref),但我想避免这种情况,因为它不是反应方式。

export function MyModal(props: MyModalProps) {
  const {onClose, isOpen, key} = props;
  // custom hook that gives me the details for the render (including the component with React.lazy)
  const {
    title, subtitle, Component, actionText, changeKey, nextKey
  } = useModalRenderDetails(key);
  const handleModalHide = (event) => {
    onClose();
  };

  return (
    <Modal
      modalTitle={title}
      modalSubtitle={subtitle}
      mainActionLabel={actionText}
      secondaryActionLabel={'Skip'}
      open={isOpen}
      onHide={event => handleModalHide(event)}
      onMainAction={childHandlerFunctionFromRef}
      onSecondaryAction={() => changeKey(nextKey)}
    >
      <React.Suspense fallback={<div>LOADING</div>}>
        <Component/>
      </React.Suspense>
    </Modal>
  );
}

can anyone provide a better solution for this situation?谁能为这种情况提供更好的解决方案? Feel free to suggest design changes if needed如果需要,请随时提出设计更改建议

This seems like a good usage of a context .这似乎是对context的一个很好的用法。

Context:语境:

Consider placing the code below in a separate file and exporting only necessary items.考虑将下面的代码放在单独的文件中并仅导出必要的项目。

First of all, define a context:首先,定义一个上下文:

const ConnectionContext = createContext();

Then create a custom provider to wrap the functions you need然后创建一个自定义提供程序来包装您需要的功能

const ConnectionContextProvider = ({ children }) => {
  const action = useRef(null);

  const setAction = (action) => {
    action.current = action;
  }

  const removeAction = () => {
    action.current = null;
  }

  const dispatchAction = () => {
    action.current && action.current()
  }

  return (
    <ConnectionContext.Provider
      value={{
        setAction,
        removeAction,
        dispatchAction,
      }}
    >
      {children}
    </ConnectionContext.Provider>
  );
}

Then wrap the useContext (this step is optional)然后包装useContext (这一步是可选的)

const useConnectionContext = () => {
  const context = useContext(context);
  return context;
}

Usage:用法:

In the parent:在父级中:

const MyModal = () => {
  const { dispatchAction } = useConnectionContext()
  // ...
  return (
    <Modal
      // ...
      onMainAction={dispatchAction}
    >
      // ...
    </Modal>
   )
}

In the child:在孩子身上:

const Child = () => {
  const { setAction, removeAction } = useConnectionContext()

  useEffect(() => {
    setAction(() => {
      // the action you want to run
    })
    return () => removeAction()
  }, [])
}

Have in mind that all code in this answer is untested.请记住,此答案中的所有代码都未经测试。

暂无
暂无

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

相关问题 如何避免子 div 响应调用父级的“onDragLeave”事件 - How to avoid a child div in react invoking a parent's `onDragLeave` event React hook如何在父级重新渲染时避免执行子组件 - React hook how to avoid executing child components when parent rerenders 如果父级有边框,当鼠标退出父级和子级时,如何避免来自父级元素的mouseover事件 - How to avoid the mouseover event from the parent element when the mouse is exiting both parent and child if the parent has a border React:处理子事件中的父事件 - React: Handling the parent event in the child 在React中,如何在父元素内的子元素中测试click事件 - In React, how to test the click event in child that's inside a parent element 如何将事件从父级传递给子级以与功能组件发生反应? - How to pass down event from parent to child in react with functional components? 如何在本机反应中启用父子按下事件? - How to enable both parent and child press event in react native? 如何允许子组件在父组件之前对路由事件做出反应? - How to allow child component to react to a routing event before the parent component? React |当多个孩子同时向父级发送事件时如何更新父级状态 - React |How to Update parent state when multiple children sending event to parent at same time 如何在单击子级时避免在父级元素上触发事件? - How can I avoid an event from firing on a parent element while clicking on the child?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM