Skip to content

Instantly share code, notes, and snippets.

@zackdotcomputer
Last active May 7, 2020 16:48
Show Gist options
  • Save zackdotcomputer/51bfd489f6abe161f7d947e57b178f95 to your computer and use it in GitHub Desktop.
Save zackdotcomputer/51bfd489f6abe161f7d947e57b178f95 to your computer and use it in GitHub Desktop.
Fixed index.tsx for reduced-context-bug - Omitting Changed Nodes
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