<div id="root"></div>
const {useContext} = React
const Context = React.createContext()

function Provider(props) {
  return (
    <Context.Provider value={{data: "haha"}}>
      {props.children}
    </Context.Provider>
  )
}

function Children() {
  const {data} = useContext(Context)
  return (
    <div>Children获取到的数据{data}</div>
  )
}

ReactDOM.render(<Provider><Children /></Provider>, document.getElementById("root"))
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js