【react】使用context进行跨级组件数据传递

官方文档:使用 Context 深层传递参数useContext

应用:无需为每层组件手动添加 props,能狗在组件树间进行数据传递的方法。

1、创建context

ContextProvider.tsx:

import React, {
    useMemo, useState } from 'react';

export interface ContextState {
   
  viewRef?: React.MutableRefObject<null>;
  activeIndex: number;
  setActiveIndex: (data: number) => void;
}

// 创建一个context
export const Context = React.createContext<ContextState>({
   
  activeIndex: 0,
  setActiveIndex: () => {
   },
});

const ContextProvider: React.FC<
  {
    children: React.ReactNode } & {
   
    viewRef: React.MutableRefObject<null>;
  }
> = ({
     children, viewRef }) => {
   
  // 使用useState监听状态变更
  const [activeIndex, setActiveIndex] = useState(0);

  const value = useMemo(() => {
   
    return {
   
      viewRef,
      activeIndex,
      setActiveIndex, // 通过context来更新对象和数据,则可以使用state的方式将其传递下去
    };
  }, [showQuizsAnswer, shotViewRef, viewRef]);

  // Context提供一个Provider组件,并将自组件包裹起来,这里的props名称必须是value
  return <Context.Provider value={
   value}>{
   children}</Context.Provider>;
};

export default ContextProvider;

要更新 context,请将其与 state 结合。在父组件中声明一个状态变量,并将当前状态作为 context value 传递给 provider。

上述写法可以参考 useContext

2、提供context

App.tsx:

import ContextProvider from './ContextProvider';

function App() {
   
  // 下面这种写法是无效的,不能在父组件中使用
  // const {  setActiveIndex, activeIndex } = React.useContext(context);
  
  return (
    // 使用provider包裹,之后所有的子组件都可以获取得到距离他最近的provider的数据
    <ContextProvider viewRef={
   viewRef}>
      <ChildComponent onChange={
   handleChange} />
    </ContextProvider>
  );
}

注意:在这里App.tsx里面不能消费context,因为App.tsx属于父组件。

useContext() 总是在调用它的组件 上面 寻找最近的 provider。它向上搜索, 不考虑 调用 useContext() 的组件中的 provider。 所以上面注释掉的写法是错误的。

3、使用context

ChildComponent.txs:

function ChildComponent() {
   
  // 使用useContext钩子访问Context的值
  const {
    setActiveIndex, activeIndex } = React.useContext(MyContext);

  // 在子组件中调用setActiveIndex函数来更改value值
  const handleInputChange = (newValue) => {
   
    setActiveIndex(newValue);
  };

  return (
    <TextInput value={
   activeIndex} onChangeText={
   handleInputChange} />
  );
}

相关推荐

  1. react使用context进行组件数据传递

    2024-01-25 06:00:04       55 阅读
  2. React Context层级组件共享状态参数、状态

    2024-01-25 06:00:04       60 阅读
  3. Vue2 父子组件进行数据传递

    2024-01-25 06:00:04       20 阅读
  4. React中的Props:传递数据组件通信

    2024-01-25 06:00:04       57 阅读
  5. React Context使用详解

    2024-01-25 06:00:04       36 阅读
  6. reactreact 使用 Context 的简单示例

    2024-01-25 06:00:04       27 阅读
  7. grpc-go通过context传递额外数据

    2024-01-25 06:00:04       55 阅读
  8. 使用React Context和Hooks在React Native中共享蓝牙数据

    2024-01-25 06:00:04       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-25 06:00:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-25 06:00:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-25 06:00:04       87 阅读
  4. Python语言-面向对象

    2024-01-25 06:00:04       96 阅读

热门阅读

  1. React中antd的使用技巧

    2024-01-25 06:00:04       51 阅读
  2. React 和 Vue的优缺点

    2024-01-25 06:00:04       54 阅读
  3. OpenCV:打开计算机视觉的魔法之门

    2024-01-25 06:00:04       59 阅读
  4. 计算机网络

    2024-01-25 06:00:04       50 阅读
  5. OpenCV处理视频文件

    2024-01-25 06:00:04       60 阅读
  6. 算法训练营Day49(动态规划10)

    2024-01-25 06:00:04       59 阅读
  7. 开源计算机视觉库OpenCV详解

    2024-01-25 06:00:04       61 阅读
  8. Kong工作原理 - 负载均衡 - 高级负载均衡

    2024-01-25 06:00:04       57 阅读
  9. Kong工作原理 - 负载均衡 - 负载均衡算法

    2024-01-25 06:00:04       63 阅读
  10. 设计模式-中介模式

    2024-01-25 06:00:04       62 阅读
  11. 探索人工智能的发展与影响

    2024-01-25 06:00:04       49 阅读