Skip to content

Instantly share code, notes, and snippets.

@SandyWyper
Last active March 27, 2021 20:25
Show Gist options
  • Save SandyWyper/95a1ec969831d4e4e55b1221d4b45a49 to your computer and use it in GitHub Desktop.
Save SandyWyper/95a1ec969831d4e4e55b1221d4b45a49 to your computer and use it in GitHub Desktop.
Component had better do what it's told!
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
@SandyWyper
Copy link
Author

This will be server-side rendered - FYI (if that matters)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment