Skip to content

Instantly share code, notes, and snippets.

@domosedov
Last active February 7, 2022 06:44
Show Gist options
  • Save domosedov/f42d8d498a8903e210a3a142b2f37ac2 to your computer and use it in GitHub Desktop.
Save domosedov/f42d8d498a8903e210a3a142b2f37ac2 to your computer and use it in GitHub Desktop.
Effector Snippets
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