【问题标题】:How do access methods from one component in a different component in react.js?react.js 中如何从不同组件中的一个组件访问方法?
【发布时间】:2018-09-24 08:38:29
【问题描述】:

我的组件中有一个navigation 和一个drawer 类(在不同的文件中)。我希望能够在navigation 类中有一个菜单按钮,它可以打开和关闭抽屉。我在drawer 类中有一个toggleStatus 方法,它将drawerStatus 的状态更改为打开/关闭。如何从navigation 类中访问toggleStatus 方法?我正在为我的所有组件使用材料设计。

【问题讨论】:

  • 你使用什么样的状态管理?如果你使用 redux,你可以触发一个触发抽屉减速器的动作。如果抽屉是导航的子项,您可以通过在抽屉上设置ref 并使用本地引用来触发更改来访问该方法
  • 在最简单的情况下,您的导航组件可以向上传递事件/回调,然后父组件可以更改 drawer 组件上的布尔属性以打开或关闭它。请参阅文档中的 Thinking in React 和 Lifting State Up。让组件相互调用彼此的方法很少适合 React 模型(尽管在某些情况下您可以为它提出理由)。
  • @JoeClay 谢谢,这是有道理的。如果组件不应该调用彼此的方法,你会建议我如何实现这一点?是否值得将抽屉和导航组合到一个组件/类中?
  • @Ryan:请参阅 sandddyyyy 的回答 - 这几乎完全符合我的描述:)
  • @Ryan 你可以试试 Redux,它让我们的生活 - React 开发者 - 更轻松。仅在一个地方管理整个应用程序状态,作为单一事实来源。简直太棒了!

标签: reactjs material-design


【解决方案1】:

如果navigation 和drawer 组件是父组件的子组件,则声明一个更新drawerStatus 的方法并将其作为prop 传递给navigation,并将状态添加到该父组件并将其作为prop 传递给@ 987654325@。是这样的:

class ParentComponent extends Component {
  constructor() {
    this.state = { isDrawerOpen: false };
  }

  toggleDrawer = () => {
    this.setState({ isDrawerOpen: !this.state.isDrawerOpen });
  }

  render() {
    return (
      <div>
        <NavigationComponent toggleDrawer={this.toggleDrawer} />
        <DrawerComponent drawerStatus={this.props.isDrawerOpen} />
      </div>
    );
  }
}

【讨论】:

    【解决方案2】:

    您可以将其作为道具传递。

    I.E. 我有这个层次结构:

    <Foo>
    <Bar/>
    </Foo>
    

    在 foo 内部,您有一个名为 toggle 的函数。

    toggle(){
        console.log('triggered')
    }
    

    您可以像这样将 toggle 传递给 Bar:

    <Bar
        toggleInBar={this.toggle.bind(this)}
    />
    

    和 inside bar 组件,您可以使用 toggleInBar 作为道具。

    this.props.toggleInBar 
    

    将触发日志记录 :)

    附:
    我故意取了一个不同的名字来更容易区分功能:)

    【讨论】:

      【解决方案3】:

      我会这样做: 1- 你的按钮类可能看起来像这样:

      import React from 'react'; // import react module
      import PropTypes from 'prop-types'; // ES6
      const Button = (props) => {
        return (
          <input
            type="button"
            onClick={props.handleClick}
            value={props.label}
          />
        );
      }
      
      // describe our expected props types
      Button.propTypes = {
        type: PropTypes.string.isRequired,
        handleClick: PropTypes.func.isRequired,
        label: PropTypes.string.isRequired
      }
      
      // export our button component.
      export default Button;
      

      你的父组件可能是这样的:

      import React from 'react'; // import react module
      import Button from './button'; // Import our button component from this directory
      // create a class which extends react component
      class Frame extends React.Component {
      
        constructor() {
          super();
          this.handleClick = this.handleClick.bind(this);
        }
      
        // our method to handle all click events from our buttons
        handleClick(event){
          const value = event.target.value; // get the value from the target element (button)
          switch (value) {
            case '1': {
               //handler for tia button with lable "1"
              break;
            }
            default: {
              // your default implementation 
              break;
            }
          }
        }
      
        // Render function to creat component to be rendered on the DOM.
        // This method must return a single parent element as you can see here. 
        // The component is wrapped around () to make it a single expression.
        render() {
          return (
            <div className="frame">
                <Button className='btn-primary' label={'1'} handleClick={this.handleClick} type='input' />
          );
        }
      }
      
      // export our frame component. To be used in our client/index.js file
      export default Frame;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-29
        • 1970-01-01
        • 1970-01-01
        • 2019-01-21
        • 1970-01-01
        • 1970-01-01
        • 2019-11-05
        • 2017-05-10
        相关资源
        最近更新 更多