Last active
July 14, 2019 13:16
-
-
Save velopert/3f4f0826cdadaaadeccdd4bd1f6b1731 to your computer and use it in GitHub Desktop.
Context Sample
This file contains hidden or 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, { createContext, useContext } from 'react'; | |
const MyContext = createContext('defaultValue'); | |
function Child() { | |
const text = useContext(MyContext); | |
return <div>안녕하세요? {text}</div> | |
} | |
function Parent() { | |
return <Child /> | |
} | |
function GrandParent() { | |
return <Parent /> | |
} | |
function ContextSample() { | |
return ( | |
<MyContext.Provider value="GOOD"> | |
<GrandParent /> | |
</MyContext.Provider> | |
) | |
} | |
export default ContextSample; |
This file contains hidden or 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, { createContext, useContext, useState } from 'react'; | |
const MyContext = createContext('defaultValue'); | |
function Child() { | |
const text = useContext(MyContext); | |
return <div>안녕하세요? {text}</div> | |
} | |
function Parent() { | |
return <Child /> | |
} | |
function GrandParent() { | |
return <Parent /> | |
} | |
function ContextSample() { | |
const [value, setValue] = useState(true); | |
return ( | |
<MyContext.Provider value={value ? 'GOOD' : 'BAD'}> | |
<GrandParent /> | |
<button onClick={() => setValue(!value)}>Click Me</button> | |
</MyContext.Provider> | |
) | |
} | |
export default ContextSample; |
This file contains hidden or 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, { createContext } from 'react'; | |
function Child({ text }) { | |
return <div>안녕하세요? {text}</div> | |
} | |
function Parent({ text }) { | |
return <Child text={text} /> | |
} | |
function GrandParent({ text }) { | |
return <Parent text={text} /> | |
} | |
function ContextSample() { | |
return <GrandParent text="GOOD" /> | |
} | |
export default ContextSample; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment