import React, { createContext, useContext, useState } from 'react' // 1.创建 Context 对象,调用 createContext 方法 const Context = createContext() function ComA() { const count = useContext(Context) return (
this is ComA
传过来的数据为:{count}
) } function ComC() { // 3.底层组件通过 useContext 函数获取数据 const count = useContext(Context) return (
this is ComC
传过来的数据为:{count}
) } function App() { const [ count, setCount ] = useState(0) return ( // 2.顶层组件通过 Context.Provider 提供数据
) } export default App