Last active
May 7, 2020 16:48
-
-
Save zackdotcomputer/51bfd489f6abe161f7d947e57b178f95 to your computer and use it in GitHub Desktop.
Fixed index.tsx for reduced-context-bug - Omitting Changed Nodes
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, { createContext, useContext } from "react"; | |
import { useWindowSize } from "react-use"; | |
const DisplayContext = createContext<"normal" | "compact">("normal"); | |
export default function Parent() { | |
const { width } = useWindowSize(); | |
let windowSizeDisplayContext = undefined; | |
if (width !== Infinity) { | |
windowSizeDisplayContext = width > 800 ? "normal" : "compact"; | |
} | |
return ( | |
<div className="container"> | |
<DisplayContext.Provider value={windowSizeDisplayContext}> | |
<Child /> | |
</DisplayContext.Provider> | |
</div> | |
); | |
} | |
const Child: React.FC = () => { | |
const displayContext = useContext(DisplayContext); | |
return ( | |
<div> | |
{displayContext && ( | |
<> | |
<p hidden={displayContext === "normal"}>Compact</p> | |
<p hidden={displayContext === "compact"}>Normal</p> | |
</> | |
)} | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment