简体   繁体   English

React-从另一个组件的onClick切换一个组件的显示

[英]React - toggle display of one component from the onClick of another component

I'm trying to build my first React project, and am currently putting together a burger nav button, and a menu which appears when clicking the nav. 我正在尝试构建我的第一个React项目,当前正在组合一个汉堡导航按钮和一个单击导航时出现的菜单。

I've broken this into two components; 我将其分为两个部分: Hamburger and MenuOverlay. 汉堡包和MenuOverlay。 The code for both is below. 两者的代码如下。

Currently I have an onClick on Hamburger toggling a class on it, but how would I also toggle the menu from that click? 目前,我在汉堡包上有一个onClick切换类,但是如何从该单击中切换菜单呢? It's hidden with display: none; 它被显示隐藏:无;没有显示。 by default. 默认。 Probably a very basic question so apologies - still trying to get my head around React. 很抱歉,这是一个非常基本的问题-仍在设法避开React。

MenuOverlay 菜单叠加

import React from 'react';
import { Link } from 'react-router';

const MenuOverlay = () => {
    return (
        <div className="menuOverlay">
            <div className="innerMenu">
                <p><Link to="/">Home</Link></p>
                <p><Link to="/">About</Link></p>
                <p><Link to="/">Contact</Link></p>
            </div>
        </div>
    );
};

export default MenuOverlay;

Hamburger 汉堡包

import React, { Component } from 'react';

class Hamburger extends Component {
    constructor(props) {
        super(props);

        this.state = { active: '' };
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
        var toggle = this.state.active === 'is-active' ? '' : 'is-active';
        this.setState({active: toggle});
    }

    render() {

        return (
            <button className={`hamburger hamburger--emphatic fadein one ${this.state.active}`} onClick={this.handleClick} type="button">
                <span className="homeMenuTextButton">Menu</span>
                <span className="hamburger-box">
                    <span className="hamburger-inner"></span>
                </span>
            </button>
        );
    }
}

export default Hamburger;

In the most simplistic form you would have a container component that wraps around both of them and manages the state of the components. 在最简单的形式中,您将拥有一个容器组件,该组件将它们包裹起来并管理组件的状态。

<MenuContainer>
 <Hamburger />
 <MenuOverlay />
</MenuContainer>

And in <MenuContainer> you would have a state of active some quick pseudocode. <MenuContainer>您将active一些快速伪代码active状态的active

class MenuContainer extends React.Component {
  constructor() {
    super();

    this.state = { active: false} 
  }

  toggleMenu = () => {

  // function that will toggle active/false
    this.setState((prevState) => {
      active: !prevState.active
    });
  }


  render() {
    return ( 
      <div>
        <Hamburger active={this.state.active} onClick={this.toggleMenu} />
        <MenuOverlay active={this.state.active} />
      </div>
    )
  }
}

so in hamburger you would just use the this.props.onClick to change the state of active and then in those corresponding components use the prop of this.props.active to determine what classes should be applied, etc. 因此,在汉堡包中,您只需使用this.props.onClick来更改active状态,然后在相应的组件中使用this.props.active确定应应用哪些类, this.props.active

Given that one element is not the parent of another element, you will have to pull up the variable keeping the toggle information up the chain of elements until it resides in one common place. 假定一个元素不是另一个元素的父元素,则必须拉起变量,将切换信息保持在元素链上,直到它位于一个公共位置。

That is, keep the "active" state variable in an ancestor of the two elements and provide to the Hamburger a callback in the props that, when called, modifies the state of that ancestor component. 也就是说,将“活动”状态变量保留在两个元素的祖先中,并向Hamburger提供props中的回调,该回调在被调用时会修改该祖先组件的状态。 At the same time, also pass the active state variable down to the MenuOverlay as a prop, and everything should work together. 同时,还将活动状态变量作为道具传递给MenuOverlay,一切都应该一起工作。

See here for more information: 浏览此处获取更多信息:

https://facebook.github.io/react/tutorial/tutorial.html#lifting-state-up https://facebook.github.io/react/tutorial/tutorial.html#lifting-state-up

Specifically, 特别,

When you want to aggregate data from multiple children or to have two child components communicate with each other, move the state upwards so that it lives in the parent component. 当您想要聚合来自多个子组件的数据或要使两个子组件彼此通信时,请向上移动状态,使其位于父组件中。 The parent can then pass the state back down to the children via props, so that the child components are always in sync with each other and with the parent. 然后,父级可以通过道具将状态传递回给子级,以便子级组件始终彼此同步并与父级保持同步。

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

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