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
const Child = () => { | |
const { count, setCount } = React.useContext(CountContext); | |
return ( | |
<div> | |
<Button onClick={() => setCount((count) => count)}> | |
Call setCount from context with same value | |
</button> | |
</div> | |
); | |
}; |
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
const Parent = () => { | |
return ( | |
<Child /> | |
); | |
}; |
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
const GrandParent = ({ children }) => { | |
return ( | |
<div>{children}</div> | |
); | |
}; |
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
const CountContext = React.createContext(null); | |
const CountContextProvider = ({ children }) => { | |
const [count, setCount] = React.useState(0); | |
return ( | |
<CountContext.Provider value={{ count, setCount }}> | |
<GrandParent>{children}</GrandParent> | |
</CountContext.Provider> | |
); | |
}; |
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
const Context = () => { | |
return ( | |
<CountContextProvider> | |
<Parent /> | |
</CountContextProvider> | |
); | |
}; |
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
const App = () => { | |
// ... | |
return ( | |
<> | |
<Page /*...*/ > | |
<ShowComments comments ={ comments } /> | |
</Page> | |
</> | |
); | |
}; |
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
const showComments = ({ comments }) => { | |
console.log("Comments is rendering"); | |
return ( | |
<ul> | |
{comments.map((comment) => ( | |
<li key={comment}>{comment}</li> | |
))} | |
</ul> | |
); | |
}; |
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
const App = () => { | |
// ... | |
return ( | |
<> | |
<Page /*...*/ > | |
{showComments({ comments })} | |
</Page> | |
</> | |
); | |
}; |
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
const Page = ({ user, children, onLogin, onAddComment }) => { | |
if (!user.isLoggedIn) { | |
return ( | |
<Layout> | |
<h3>Please log in to see and post comments</h3> | |
<PasswordInput/> | |
<button onClick={onLogin}>Login!</button> | |
</Layout> | |
); | |
} |
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
const PasswordInput = () => <input type="password" />; | |
const CommentInput = () => <input type="text" />; |