简体   繁体   English

如何访问另一个组件的状态

[英]How to access state of another component

I have home component with header component how may I access the header states in home component here is the example 我有带有标头组件的home组件,如何在home组件中访问标头状态,这是示例

Header.JS Header.JS

class Header extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            pageNumber: 1
        }
    }
  render() {
    return (
      <div className="shopping-list">
        <ul>
          <li>Instagram</li>
          <li>WhatsApp</li>
          <li>Oculus</li>
        </ul>
      </div>
    );
  }
}

Home.js Home.js

import React, {Component} from 'react';
import Header from './header';

class App extends Component {

    render() {
        return (
            <div>
             <Header /> // I need to use the states of Header in this component
            </div>
        )
    }
}

export default App;

I need to use the states of Header in Home component, is that possible? 我需要在Home组件中使用Header的状态,这可能吗?

Does this way is good for performance? 这样对性能有好处吗? Thank you 谢谢

You need to use state lifting . 您需要使用state lifting

Define a method eg liftTheState in Header component which will lift the state of Header component by calling Home component method with required data. 定义一个方法例如liftTheStateHeader部件,其将解除的状态Header通过调用组件Home与所需的数据组件的方法。

class Header extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            pageNumber: 1
        }
    }

  liftTheState=()=>{

      this.props.callHomeMethod(this.state.pageNumber);
  }
  render() {
    //...
  }
}

In Home : 在家 :

Define a method eg callHomeMethod in Home component which will hold the Header component data and passed the method as a prop to Header component Home组件中定义一个方法,例如callHomeMethod ,它将保存Header组件数据并将该方法作为prop传递给Header组件

class App extends Component {

    callHomeMethod = (someDataFromHeader)=>{


    }
    render() {
        return (
            <div>
             <Header  callHomeMethod = {this.callHomeMethod}/> // pass Home method here used for state lifting.
            </div>
        )
    }
}

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

相关问题 如何访问另一个组件中的当前状态? - How to access a current state in another component? 如何从另一个组件访问一个组件的状态 - How to access one component's state from another component 如何从 next.js 中的另一个组件访问组件的状态? - How to access a state of an component from another component in next.js? 我如何从另一个 React 组件访问 redux 状态? - How do i access redux state from another react component? 当我处于另一个组件中时如何访问状态? - How to access to a state when I'm in another component? React:如何通过另一个组件访问和更改状态/属性? - React: How to access and change state/atributes via another component? 如何访问在另一个组件中使用链接发送的 state - How to access the state that was sent using Link to in another component 从另一个组件反应访问 state - React access state from another component 反应:redux-axios:如何访问一个组件可被另一组件访问的状态? - React: redux-axios: How to access the state of one component accessible to another component? 如何在另一个调用 this.setState() 的函数内的函数中访问组件的状态? - How do I access a component's state in a function that is inside another function that calls this.setState()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM