Created
March 12, 2020 12:07
-
-
Save penx/39a140ca1fd60e7ac85b8c189bdbcff2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, createContext, useContext } from "react"; | |
const SetContext = createContext(); | |
const ValueContext = createContext(); | |
const Provider = props => { | |
const [value, set] = useState("initial"); | |
return ( | |
<SetContext.Provider value={set}> | |
<ValueContext.Provider value={value} {...props} /> | |
</SetContext.Provider> | |
); | |
}; | |
const Value = () => { | |
const value = useContext(ValueContext); | |
return <div>{value}</div>; | |
}; | |
const SetValue = () => { | |
const set = useContext(SetContext); | |
return <button onClick={() => set(Date.now())}>Change</button>; | |
}; | |
export default function App() { | |
return ( | |
<Provider> | |
<Value /> | |
<SetValue /> | |
</Provider> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment