React 中 Context 和 contextType的使用

ReactContext 的使用

context

Context 提供了一种方式,能够让数据在组件树中传递时不必一级一级的手动传递

一般情况下,数据在组件中,要一级一级的传递,单向数据流,比如Parent组件中的theme值,需要在Item组件中使用,就需要我们从Parent中向下传递,
但当我们有了Context后,我们就不需要一级一级传递了

    Parent(theme=red)
        List(theme=red)
            Item(theme=red)
            
    ThemeContext.Provider value={'red'}
        List
            ThemeContext.Customer  (theme) => { theme }

具体如何使用呢,看下面例子


   
    import React, {
    createContext } from 'react';

    // 创建Context的唯一方法
    const ThemeContext = createContext()
    
    
    
    class App extends React.Component {
   
      render () {
   
        return (
          // 使用 Context.Provider 包裹后续组件,value 指定值 
          <ThemeContext.Provider value={
   'red'}>
            <Middle></Middle>
          </ThemeContext.Provider>
        )
      }
    }
    
    class Bottom extends React.Component {
   
      render () {
   
        return (
          // Context.Consumer Consumer消费者使用Context得值
          // 但子组件不能是其他组件,必须渲染一个函数,函数的参数就是Context得值
          <ThemeContext.Consumer>
            {
   
              theme => <h1>ThemeContext 的 值为 {
   theme}</h1>
            }
          </ThemeContext.Consumer>
        )
      }
    }
    
    
    class Middle extends React.Component {
   
      render () {
   
        return <Bottom></Bottom>
      }
    }
    
    export default App;


当 Provider 提供的值更改时,Consumer 必须重新渲染

    import React, {
    createContext } from 'react'
  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
React Context 结合 Reducer 可以实现状态管理和状态更新的功能。Reducer 是一个纯函数,接收当前的状态和一个 action,返回一个新的状态。通过 Context 和 Reducer 的结合使用,可以将状态和状态更新逻辑分离到不同的文件,使得组件的代码更加简洁和清晰。 下面是一个示例代码: ```jsx // 创建一个 context 对象 const MyContext = React.createContext(); // 定义 reducer 函数 function reducer(state, action) { switch (action.type) { case "increment": return { count: state.count + 1 }; case "decrement": return { count: state.count - 1 }; default: throw new Error(); } } // 父组件 function ParentComponent() { const [state, dispatch] = useReducer(reducer, { count: 0 }); // 将 reducer 的 dispatch 函数作为属性传递给子组件 return ( <MyContext.Provider value={{ state, dispatch }}> <ChildComponent /> </MyContext.Provider> ); } // 子组件 function ChildComponent() { // 使用 useContext 钩子获取父组件提供的 context 对象 const { state, dispatch } = useContext(MyContext); return ( <div> <p>当前的计数:{state.count}</p> <button onClick={() => dispatch({ type: "increment" })}>增加</button> <button onClick={() => dispatch({ type: "decrement" })}>减少</button> </div> ); } ``` 在上面的代码,父组件使用 `useReducer` 钩子创建了一个状态以及一个 dispatch 函数。将状态和 dispatch 函数通过 Context 提供给子组件。在子组件,通过 `useContext` 钩子获取父组件提供的 state 和 dispatch 对象。当用户点击子组件的增加或减少按钮时,会调用 dispatch 函数并传入对应的 action 对象,从而触发状态更新,更新后的状态会自动传递给所有使用了该 Context 的组件,实现了状态的管理和更新。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值