React Context

2023-04-21  本文已影响0人  Petricor

前言:React Context是什么?

示意图

1.0 在类组件中使用Context

1.1 在parment.jsx 父组件中创建 Context

import React from 'react';
import Child from './child.jsx'

export const UserContext = React.createContext(); //创建Context 定义为“UserContext ”,并且输出


1.1.1 在parment.jsx 引入需要传参的组件,并且挂载 contextType
import React from 'react';
import Child from './child.jsx'
import GrandChild from './grandChild.jsx'

export const UserContext = React.createContext(); //创建Context 定义为“UserContext ”,并且输出

GrandChild .contextType = UserContext;   //需要传参的组件 

1.1.2 在parment.jsx 通过 Provider(发布者)value 属性传递参数
import React from 'react';
import Child from './child.jsx'
import GrandChild from './grandChild.jsx'

export const UserContext = React.createContext(); //创建Context 定义为“UserContext ”,并且输出

GrandChild .contextType = UserContext;   //需要传参的组件 
//父组件
export default class Parment extends React.Component {
    static contextType
    render() {
        //2, Context.Provider 标签注释掉,则传入默认的 
        return (
            <div>
                <UserContext.Provider value={{ userName: "zhangsang" }}>
                    <Child />
                </UserContext.Provider>
            </div>
        );
    }
}

1.2 在child.jsx 组建中正常引用 grandChild.jsx 组件

import React from 'react';
import GrandChild from './grandChild.js'  //引入组件

//用户信息 - 二级页面
export default class Child extends React.Component {
    render() {
        return (<GrandChild />);
    }
}

1.3.0 引入在parment.jsx 中定义的 context

import React from 'react';
import  { UserContext } from './parment.jsx'
1.3.1 通过Consumer(消费者)来获取传输的参数
import React from 'react';
import  { UserContext } from './parment.jsx'

class GrandChild extends React.Component {
    render() {
        // console.log(this.context, '===userBaseInfo');
        return (
            <div>
                用户名:{this.context.userName}
                <br />
                <UserContext.Consumer>
                    {(value) => {
                        return (<span>{value.userName}</span>);
                    }}
                </UserContext.Consumer>
            </div>
        );
    }
}

export default  GrandChild 

注: 如果想要在Consumer外使用参数,需要用this.context 进行获取

2.0 在hook 中使用Context

2.1 在parment.jsx 父组件中创建 Context并且挂载传递参数
import React from 'react';
import Child from './child'

export const hookContext = React.createContext()

export default function UContext() {
    return (
      <div>
        // 当Provider 标签不在的时候,会渲染默认值
        <ThemeContext.Provider value={{ userName: "zhangsang" }}>
            <Child  />
        </ThemeContext.Provider>
      </div>
    );
}

2.2 在child.jsx 组建中正常引用 grandChild.jsx 组件
import React from 'react';
import GrandChild from './grandChild'

export default function Toolbar() {
    return (
        <div>
            <GrandChild />
        </div>
    );
}
2.3.0 引入在parment.jsx 中定义的 context 和useContext
import React, { useContext } from 'react';
import { hookContext } from './parment.jsx'

2.3.1 获取和使用参数
import React, { useContext } from 'react';
import { hookContext } from './parment.jsx'

export default function GrandChild() {
    const  value  = useContext(hookContext);
    return (
       <span>{value.userName}</span>
    );
}

结语

上一篇下一篇

猜你喜欢

热点阅读