如何在react中从父组件调用子组件回调事件

发布于 2025-01-19 16:44:33 字数 1201 浏览 2 评论 0原文

你好,我是 React 的初学者,我在我的项目中使用 Fluent UI。 我计划使用 Fluent UI 中的面板控件并将其作为通用组件,以便我可以重用它。我使用以下代码

import * as React from 'react';
import { DefaultButton } from '@fluentui/react/lib/Button';
import { Panel } from '@fluentui/react/lib/Panel';
import { useBoolean } from '@fluentui/react-hooks';

export const PanelBasicExample: React.FunctionComponent = () => {
  const [isOpen, { setTrue: openPanel, setFalse: dismissPanel }] = useBoolean(false);

  return (
    <div>
      
      <Panel
        headerText="Sample panel"
        isOpen={isOpen}
        onDismiss={dismissPanel}
        // You MUST provide this prop! Otherwise screen readers will just say "button" with no label.
        closeButtonAriaLabel="Close"
      >
        <p>Content goes here.</p>
      </Panel>
    </div>
  );
};

https://developer.microsoft.com/en-us/ Fluentui#/controls/web/panel#best-practices

我删除示例中的

所以我的问题是如何从任何其他组件打开或关闭此面板?

Hi I am a beginner in React, I am using Fluent UI in my project .
I am planning to use Panel control from Fluent UI and make that as common component so that I can reuse it.I use bellow code

import * as React from 'react';
import { DefaultButton } from '@fluentui/react/lib/Button';
import { Panel } from '@fluentui/react/lib/Panel';
import { useBoolean } from '@fluentui/react-hooks';

export const PanelBasicExample: React.FunctionComponent = () => {
  const [isOpen, { setTrue: openPanel, setFalse: dismissPanel }] = useBoolean(false);

  return (
    <div>
      
      <Panel
        headerText="Sample panel"
        isOpen={isOpen}
        onDismiss={dismissPanel}
        // You MUST provide this prop! Otherwise screen readers will just say "button" with no label.
        closeButtonAriaLabel="Close"
      >
        <p>Content goes here.</p>
      </Panel>
    </div>
  );
};

https://developer.microsoft.com/en-us/fluentui#/controls/web/panel#best-practices

I remove <DefaultButton text="Open panel" onClick={openPanel} /> from the example .

So my question is how can I open or close this panel from any other component ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2025-01-26 16:44:33

我会为此使用 React useState 钩子。

在想要渲染面板的组件中创建一个状态,例如

const [openPanel, setOpenPanel] = useState({
     isOpen: false,
     headerText: ''
})

,您将从按钮打开它

  <Button onClick={() => setOpenPanel({
           isOpen: true,
           headerText: 'Panel-1'
       })
     }> Open me ! </Button>

然后将状态作为道具传递给面板组件

    <PanelBasicExample openPanel={openPanel} setOpenPanel={setOpenPanel} /> 

in PanelBasicExample component you can extract the props and use it.

    export const PanelBasicExample(props) => {
      const {openPanel, setOpenPanel} = props
      const handleClose = () => {setOpenPanel({isOpen: false})}
     return (
    <div>  
      <Panel
        headerText={openPanel.headerText}
        isOpen={openPanel.isOpen}
        onDismiss={() => handleClose}
        // You MUST provide this prop! Otherwise screen readers will just say "button" with no label.
        closeButtonAriaLabel="Close"
      >
        <p>Content goes here.</p>
      </Panel>
    </div>
  );
}

I would use React useState hook for this.

Make a state in the component that you want render the Panel like

const [openPanel, setOpenPanel] = useState({
     isOpen: false,
     headerText: ''
})

Lets say for example you will open it from button

  <Button onClick={() => setOpenPanel({
           isOpen: true,
           headerText: 'Panel-1'
       })
     }> Open me ! </Button>

Then pass the state as props to the Panel component

    <PanelBasicExample openPanel={openPanel} setOpenPanel={setOpenPanel} /> 

in PanelBasicExample component you can extract the props and use it.

    export const PanelBasicExample(props) => {
      const {openPanel, setOpenPanel} = props
      const handleClose = () => {setOpenPanel({isOpen: false})}
     return (
    <div>  
      <Panel
        headerText={openPanel.headerText}
        isOpen={openPanel.isOpen}
        onDismiss={() => handleClose}
        // You MUST provide this prop! Otherwise screen readers will just say "button" with no label.
        closeButtonAriaLabel="Close"
      >
        <p>Content goes here.</p>
      </Panel>
    </div>
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文