Skip to content

Instantly share code, notes, and snippets.

this is a automated and typeful version of https://github.com/pmndrs/zustand/blob/bf9d0922fe9292c2399b9594eafe9304016b52d9/docs/guides/initialize-state-with-props.md I'm not sure if it is a great approach of doing it. because I don't have much knowledge of how SSR codes should work. That's why I placed it in gist, not a repository. if you have any idea of improving it or if you think there are better ways, I'd so glad to know :)

bear.store.ts
export const createBearStore = makeStoreCreator((set) => ({
  bears: 0,
  increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
  removeAllBears: () => set({ bears: 0 }),
@arashi-dev
arashi-dev / useComponentWillMount.js
Last active October 22, 2021 21:17
React hook compatible with NextJS and SSR. Runs before rendering but in the client side. Returns everything you return in the callback
const useComponentWillMount = (cb) => {
const isMountedRef = useRef(false)
const resultRef = useRef()
if (!isMountedRef.current && typeof window !== "undefined") {
resultRef.current = cb()
}
isMountedRef.current = true