(1)需求最近在学习React,学习了ReactHook,做了useContextDemo。(2)使用useContext的目的是为了方便祖孙组件之间的值传递。//爸爸组/**@Author:ArdenZhao*@Date:2022-04-1609:48:50*@LastEditTime:2022-04-1615:14:57*@FilePath:/react-ts/src/components/react/9-ContextProvider.js*@Description:文件信息*/importReact,{useState,createContext}from'react';exportconstcontext=createContext({});exportfunctionContextProvider({children}){const[年龄,设定年龄]=useState(18);constconstVal={age,setAge,addAge:()=>setAge(age+1),}return{children}};//孙子组件/**@Author:ArdenZhao*@Date:2022-04-1609:30:20*@LastEditTime:2022-04-1615:05:44*@FilePath:/react-ts/src/components/react/9-Hook-useContext.js*@Description:孙组组件价值*/import{useContext}from'react';import{Button}from'antd';import"antd/dist/antd.css";import{context,ContextProvider}来自'./9-ContextProvider';functionHookUseContext(props){const{age,addAge}=useCon文本(上下文);constclickX=()=>{addAge(年龄);}return(Learn,{props.name}
实现计数器方法:{age}
useStateClick+1 );}//eslint-disable-next-lineimport/no-anonymous-default-exportexportdefault()=>{return()}写在学习路上的最后一句话,经常懈怠《有想学技术需要监督的同学嘛~》https://mp.weixin.qq.com/s/Fy...如果有需要的小伙伴可以加我微信:learningisconnectingorfollowmeon公众号:国兴聊成长(我会分享成长的方法)