React使用context进行跨级组件数据传递
应用:无需为每层组件手动添加 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} /> ); }
到此这篇关于React使用context进行跨级组件数据传递的文章就介绍到这了,更多相关React context组件数据传递内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
解决React报错Parameter 'props' implicitly&nb
这篇文章主要为大家介绍了React报错Parameter 'props' implicitly has an 'any' type的解决处理方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-12-12react component changing uncontrolled in
这篇文章主要为大家介绍了react component changing uncontrolled input报错解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-12-12React 使用browserHistory项目访问404问题解决
这篇文章主要介绍了React 使用browserHistory项目访问404问题解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-06-06如何去除富文本中的html标签及vue、react、微信小程序中的过滤器
这篇文章主要介绍了如何去除富文本中的html标签及vue、react、微信小程序中的过滤器,在vue及react中经常会遇到,今天通过实例代码给大家讲解,需要的朋友可以参考下2018-11-11
最新评论