Last active
February 7, 2022 06:44
-
-
Save domosedov/f42d8d498a8903e210a3a142b2f37ac2 to your computer and use it in GitHub Desktop.
Effector Snippets
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 * as React from "react"; | |
import type { Scope } from "effector"; | |
import { fork, serialize } from "effector"; | |
let clientScope: Scope; | |
const initializeScope = (initialData: Record<string, unknown>) => { | |
let scope = fork({ | |
values: { | |
...(clientScope ? serialize(clientScope) : {}), | |
...initialData, | |
}, | |
}); | |
if (typeof window !== "undefined") { | |
clientScope = scope; | |
} | |
return scope; | |
}; | |
export const useScope = (initialData = {}) => | |
React.useMemo(() => initializeScope(initialData), [initialData]); | |
export const getClientScope = (): Scope | undefined => clientScope; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment