react 跨组件通信

191 阅读1分钟
  • 函数氏组件

    • 1、使用 UserContext = React.createContext
    • 2、在最外层 UserContext.Provider value= {this.state}
    • 3、 UserContext.Consumer 回调函数
  • 类组件

    • 1 UserContext = React.createContext

    • 2 在最外层 UserContext.Provider value= {this.state}

    • 3 类组件.contextType = UserContxt

    • 4 类组件直接使用this.context