javascript - React - 如何在另一个组件中显示来自图像 onClick 事件的组件?

标签 javascript reactjs

我有 3 个 React 组件:

-Home - 图片显示时需要在此处显示组件 从 Header 组件点击

-Header- 包含将被点击的图像标签 显示 AllSites 组件。

-AllSites - Image为Home组件时需要显示的组件 在 Header 组件中单击。

标题

     export class Header extends React.Component<any, any> {
            private readonly searchServerUrl = "";
            private appButtonElement: HTMLElement;

            constructor(props: any) {
                super(props);

                this.state = { showAppMenu: false };
            }

            render() {
                const { showAppMenu } = this.state;
                const { className, navItems, singleColumn, appItems } = this.props;

                return (
                    <header className={className}>
                        <div className="app-icon">
                            <button className="nav-button" onClick={() => this.toggleAppMenu()} ref={(menuButton: any) => this.appButtonElement = menuButton}><i className="ms-Icon ms-Icon--Waffle" aria-hidden="true"></i></button>
                        </div>

    ***When image is clicked, show the <AllSites/> component in the HomeComponent below.*** 
                        <img src="/Styles/Images/logo/loop-logo-white.png" className="nav-logo" onClick={} />


 {showAppMenu ? <ApplicationMenu navItems={appItems} targetElement={this.appButtonElement} onDismiss={() => this.onDismiss()} /> : null}
                    <div className="nav-container"><TopNavigation classNam

e={className} navItems={navItems} singleColumn={singleColumn} /></div>
                        <div className="search-container">
                            <SearchBox onSearch={(searchTerm: string) => this.executeSearch(searchTerm)} />
                        </div>
                    </header>
                );
            }

首页

        export class HomeComponent extends React.Component<any, any> {
            constructor(props: any) {
                super(props);
                this.state = { navItems: [], appItems: [], singleColumnLayout: false, showAllSites: false };
            }

            componentDidMount() {
                this.checkWidth();
                window.addEventListener("resize", this.checkWidth.bind(this));

                this.fetchNavigation()
                    .then(nav => this.setState({ navItems: nav }));

                this.fetchAppIcons()
                    .then(icons => this.setState({ appItems: icons }));
            }


            componentWillUnmount(): void {
                window.addEventListener("resize", this.checkWidth.bind(this));
            }

            render() {
                const { navItems, appItems, singleColumnLayout } = this.state;

                return (
                    <Fabric>
                        <Header navItems={navItems} appItems={appItems} singleColumn={singleColumnLayout} />
                        <div className="main-container">
                            <AlertBar />
                            <div className="main-content">


                                <div className="ms-Grid">

点击图像标签时,我需要渲染 <AllSites/>组件在这里

                                    <Hero singleColumn={singleColumnLayout} />
                                    <div className="ms-Grid-row">
                                        <div className="ms-Grid-col ms-sm12 ms-xl4 webpart-container">
                                            <UpcomingEvents />
                                        </div>
                                        <div className="ms-Grid-col ms-sm12 ms-xl4 webpart-container">
                                            <EmployeeNews />
                                        </div>
                                        <div className="ms-Grid-col ms-sm12 ms-xl4 webpart-container">
                                            <div className="ms-Grid-row">
                                                <div className="ms-Grid-col ms-sm12 webpart-container">
                                                    <IndustryNews />
                                                </div>
                                                <div className="ms-Grid-col ms-sm12 webpart-container">
                                                    <Quote />
                                                </div>

                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <Footer navItems={navItems} />
                            </div>
                        </div>
                    </Fabric>
                );
            }

最佳答案

在最简单的方法中,您需要为您的 HomeHeader 设置一个公共(public)父组件,该组件将为它们保存一些共享状态,并传递一个回调来更新它state 作为 Header 的 Prop 。在共享状态下,您需要一个标志来负责显示/隐藏 AllSites 组件,您将把这个标志作为 Prop 传递给 Home

你可以看到一个基本的例子here

如果需要更高级的状态管理方案,可以查看redux library

关于javascript - React - 如何在另一个组件中显示来自图像 onClick 事件的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47774770/

相关文章:

reactjs - Typescript中的自定义 react 钩子(Hook)useToggle

reactjs - 如何让 MobX Decorators 与 Create-React-App v2 一起使用?

javascript - 如何获取数组中元素的特定部分

javascript - 如何在 TypeScript 中为图像添加路径别名?

javascript - 如何让一个方法等待另一个方法完成?

javascript - 当专注于 iPhone 上的只读输入时,事件停止触发

javascript - 如何在 react 中单击按钮时获取输入字段值?

javascript - 如果没有互联网连接,我想 toast ,不知道该怎么做

javascript - 如何找到所有没有角色的成员?不和谐.js v12

javascript - 更改状态输入,它是存在于 JSON 数组中的 JSON 对象