import React, { useContext, useState } from 'react' import Context from './context' 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