学习React-16-useContext

使用 useContext 的基本概念

useContext 是 React Hooks 的一部分,用于在函数组件中访问 React 的 Context。Context 提供了一种在组件树中共享数据的方式,避免了逐层传递 props 的繁琐。

创建 Context

需要先通过 React.createContext 创建一个 Context 对象,并设置默认值(可选):

javascript 复制代码
const MyContext = React.createContext(defaultValue);

提供 Context 值

在父组件中使用 Context.Provider 包裹子组件,并通过 value 属性传递数据:

javascript 复制代码
<MyContext.Provider value={/* 共享的数据 */}>
  <ChildComponent />
</MyContext.Provider>

在子组件中消费 Context

在函数组件中通过 useContext 直接获取 Context 的值:

javascript 复制代码
const value = useContext(MyContext);

小栗子

以下是一个完整的示例,展示如何创建、提供和使用 Context:

ts 复制代码
import React, {useState, useContext} from 'react'

interface ThemeContextType {
    theme: string
    setTheme: (theme: string) => void
}
// 声明上下文
const MyContext = React.createContext<ThemeContextType>({} as ThemeContextType)

const style = {
    width: '100px',
    height: '100px',
    backgroundColor: 'black',
    marginTop: '10px'
}

const Child = () => {
    const theme = useContext(MyContext)
    return (
        <div>
            子组件
            <button onClick={() => theme.setTheme('dark')}>子组件切换</button>
            <div style={{...style, backgroundColor: theme.theme == 'light'? 'green': 'black'}}>
            
        </div>
        </div>
        
    )
}

const Parent = () => {
    // 使用上下文
    const theme = useContext(MyContext)
    return (
        <div>
            父组件
            <button onClick={() => theme.setTheme('light')}>父组件切换</button>
            <div style={{...style, backgroundColor: theme.theme == 'light'? 'green': 'black'}}></div>
            <Child />
        </div>
    )
}

export const App = () => {
    const [theme, setTheme] = useState('light')
    return (
        <div>
            <button onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>主题切换</button>
            <MyContext.Provider value={{theme, setTheme}}>
                <Parent />
            </MyContext.Provider>
        </div>
    )
}

export default App

注意:上面的栗子是React18的语法,React19中已经不需要使用Provider。

ts 复制代码
// React 19的语法
<MyContextvalue={{theme, setTheme}}>
      <Parent />
</MyContext>

多 Context 使用

一个组件可以消费多个 Context,只需多次调用 useContext

javascript 复制代码
const user = useContext(UserContext);
const theme = useContext(ThemeContext);

性能优化

避免因不必要的重新渲染导致性能问题,可以将 Context 的值通过 useMemouseState 进行优化:

javascript 复制代码
const [value, setValue] = useState(initialValue);
const memoizedValue = useMemo(() => ({ value, setValue }), [value]);

return (
  <MyContext.Provider value={memoizedValue}>
    {/* 子组件 */}
  </MyContext.Provider>
);

注意事项

  • 默认情况下,useContext 会监听 Context 值的变化,即使父组件使用 React.memoshouldComponentUpdate,子组件仍会重新渲染。
  • 如果 Context 的 value 是一个新对象(如 value={``{ theme }}),每次渲染都会触发子组件更新,应避免直接传递内联对象。
  • 如果重复嵌套使用并且使用的值是相同的则里层的值会覆盖外层的值。
相关推荐
放下华子我只抽RuiKe59 分钟前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架
LuminousCPP44 分钟前
数据结构 - 线性表第四篇:C 语言通讯录优化升级全记录(踩坑 + 思考)
c语言·开发语言·数据结构·经验分享·笔记·学习
魔法阵维护师1 小时前
从零开发游戏需要学习的c#模块,第十四章(保存和加载)
学习·游戏·c#
IT_陈寒2 小时前
Redis缓存击穿把我整不会了,原来还有这手操作
前端·人工智能·后端
_李小白2 小时前
【android opencv学习笔记】Day 17: 目标追踪(MeanShift)
android·opencv·学习
一只机电自动化菜鸟2 小时前
一建机电备考笔记(40) 建筑机电施工—排水管道施工(含考频+题型)
经验分享·笔记·学习·职场和发展·课程设计
idcu2 小时前
深入 Lyt.js 组件系统:L2 渲染引擎层的核心
前端·typescript
2301_818730562 小时前
numpy的学习(笔记)
学习·numpy
这是程序猿2 小时前
Spring Boot自动配置详解
java·大数据·前端
文心快码BaiduComate2 小时前
干货|Comate Harness Engineering工程实践指南
前端·后端·程序员