日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

React 中的 context使用方法

作者:多樂_π 更新時(shí)間: 2023-07-15 編程語言

什么是上下文?

全局變量就是全局的上下文,全局都可以訪問到它;上下文就是你運(yùn)行一段代碼,所要知道的所有變量。

三大件

  1. React.createContext(): context 的初始化
  2. provider: 提供context的父組件,用來包裹需要context的子組件的
  3. 子組件獲得context的方法
  4. 代碼如5:
  5. //1.要先創(chuàng)建createContex
    export const UserContext = React.createContext(defaultValue);
    
    
    //2.Provider 指定使用的范圍
    
    render() {
        return (
          <UserContext.Provider value={this.state} >
            {this.props.children}
          </UserContext.Provider>
        );
      }
    
    
    
    
    
    
    //最后在子組建使用
    
    import { withContext } from "../../contexts";
    import { UserState, UserContext } from "../../contexts/user";
    
    class TestComponent extends React.Component<UserState, State> {
    
    
    
    }
    
    export default withContext(UserContext)(TestComponent);

原文鏈接:https://blog.csdn.net/weixin_45481456/article/details/131635479

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新