Last active
March 27, 2021 20:25
-
-
Save SandyWyper/95a1ec969831d4e4e55b1221d4b45a49 to your computer and use it in GitHub Desktop.
Component had better do what it's told!
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, useEffect } from "react" | |
import { get } from "lodash" | |
import useCallbackOnStateChange from "../lib/useCallbackOnStateChange" | |
const SomeComponent = ({ locationData }) => { | |
const [isGrid, setIsGrid] = useState(() => | |
get(locationData, "state.isGrid", false) | |
) | |
const [counter, setCounter] = useState(0) | |
useEffect(() => { | |
setCounter(counter + 1) | |
console.log("useEffect... on page load", { isGrid }, { counter }) | |
}, []) | |
useCallbackOnStateChange(() => { | |
setCounter(counter + 1) | |
console.log("the grid state has changed", { isGrid }, { counter }) | |
}, [isGrid]) | |
return ( | |
<section> | |
<button onClick={() => setIsGrid(!isGrid)}>Change grid state</button> | |
<br /> | |
<br /> | |
<div>normal layout {isGrid && "x"}</div> | |
<div>grid layout {!isGrid && "x"}</div> | |
</section> | |
) | |
} | |
export default SomeComponent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will be server-side rendered - FYI (if that matters)