1. utils下的context.js
import React ,{ createContext} from "react" //createContext 是创建一个上下文对象
export const dateContext= createContext({}) // 创建了一个上下文对象
export const {Provider,Consumer} = dateContext
2.爷爷组件
import {Provider} from "../utils/context"
<Provider value={obj}>
<Appson sendfn={sendfn}></Appson>
</Provider>
3.孙子组件
import React from 'react'
import {useContext} from "react" //
import {dateContext} from "../utils/context"
function Appgrand() {
let contextValue = useContext(dateContext)
console.log(contextValue,111);
return (
<div> 我是爷爷组件传递来的数据{contextValue.name}</div>
)
}
export default Appgrand