使用 context 进行跨组件传递数据

2022-05-10  本文已影响0人  张_何
Context
React.createContext
Context.Provider
Class.contextType
Context.Consumer
类组件使用 context 进行跨组件传递数据
具体步骤如下:
const UserContext = React.createContext({ //设置初始值,如果没找到 Provider 就使用初始值
  nickname: "张三",
  level: 0
})
<UserContext.Provider value={this.state}>
   <Profile />
</UserContext.Provider>
<div>
   <h2>用户昵称: {this.context.nickname}</h2>
   <h2>用户等级: {this.context.level}</h2>
</div>

完整代码如下:

import React, { Component } from 'react';

// 创建Context对象
const UserContext = React.createContext({
  nickname: "张三",
  level: 0
})

class ProfileHeader extends Component {
  render() {
    return (
      <div>
        <h2>用户昵称: {this.context.nickname}</h2>
        <h2>用户等级: {this.context.level}</h2>
      </div>
    )
  }
}

ProfileHeader.contextType = UserContext;

function Profile(props) {
  return (
    <div>
      <ProfileHeader />
      <ul>
        <li>设置1</li>
        <li>设置2</li>
      </ul>
    </div>
  )
}

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      nickname: "kobe",
      level: 99
    }
  }

  render() {
    return (
      <div>
        <UserContext.Provider value={this.state}>
          <Profile />
        </UserContext.Provider>
      </div>
    )
  }
}
class ProfileHeader extends Component {
  render(){
    return (
      <UserContext.Consumer>
        {
          value => {
            return (
              <ThemeContext.Consumer>
                {
                  theme => {
                    return (
                      <div>
                        <h2 style={{color: theme.color}}>用户昵称: {value.nickname}</h2>
                        <h2>用户等级: {value.level}</h2>
                        <h2>颜色: {theme.color}</h2>
                      </div>
                    )
                  }
                }
              </ThemeContext.Consumer>
            )
          }
        }
      </UserContext.Consumer>
    )
  }
}
函数式组件使用 context 进行跨组件传递数据
具体步骤如下:
const UserContext = React.createContext({ //设置初始值
  nickname: "张三",
  level: 0
})
<UserContext.Provider value={this.state}>
   <Profile />
</UserContext.Provider>
function ProfileHeader() {
  return (
    <UserContext.Consumer>
      {
        value => {
          return (
            <div>
              <h2>用户昵称: {value.nickname}</h2>
              <h2>用户等级: {value.level}</h2>
            </div>
          )
        }
      }
    </UserContext.Consumer>
  )
}
const ThemeContext = React.createContext({
  color: "black"
})
<UserContext.Provider value={this.state}>
  <ThemeContext.Provider value={{ color: "red" }}>
    <Profile />
  </ThemeContext.Provider>
</UserContext.Provider>
function ProfileHeader() {
  return (
    <UserContext.Consumer>
      {
        value => {
          return (
            <ThemeContext.Consumer>
              {
                theme => {
                  return (
                    <div>
                      <h2 style={{color: theme.color}}>用户昵称: {value.nickname}</h2>
                      <h2>用户等级: {value.level}</h2>
                      <h2>颜色: {theme.color}</h2>
                    </div>
                  )
                }
              }
            </ThemeContext.Consumer>
          )
        }
      }
    </UserContext.Consumer>
  )
}
import React, { Component } from 'react';

// 创建Context对象
const UserContext = React.createContext({
  nickname: "aaaa",
  level: -1
})

const ThemeContext = React.createContext({
  color: "black"
})

function ProfileHeader() {
  // jsx -> 嵌套的方式
  return (
    <UserContext.Consumer>
      {
        value => {
          return (
            <ThemeContext.Consumer>
              {
                theme => {
                  return (
                    <div>
                      <h2 style={{color: theme.color}}>用户昵称: {value.nickname}</h2>
                      <h2>用户等级: {value.level}</h2>
                      <h2>颜色: {theme.color}</h2>
                    </div>
                  )
                }
              }
            </ThemeContext.Consumer>
          )
        }
      }
    </UserContext.Consumer>
  )
}

function Profile(props) {
  return (
    <div>
      <ProfileHeader />
      <ul>
        <li>设置1</li>
        <li>设置2</li>
        <li>设置3</li>
        <li>设置4</li>
      </ul>
    </div>
  )
}

export default class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      nickname: "kobe",
      level: 99
    }
  }

  render() {
    return (
      <div>
        <UserContext.Provider value={this.state}>
          <ThemeContext.Provider value={{ color: "red" }}>
            <Profile />
          </ThemeContext.Provider>
        </UserContext.Provider>
      </div>
    )
  }
}

上一篇 下一篇

猜你喜欢

热点阅读