From 472f2fb13aebce3389ff7aeebad12fe0d595afb4 Mon Sep 17 00:00:00 2001 From: ck-tekton <33156025+charkour@users.noreply.github.com> Date: Fri, 16 Dec 2022 15:33:33 -0500 Subject: [PATCH] 0.0.1 --- .gitignore | 3 +++ package.json | 22 ++++++++++++++++++++++ src/index.tsx | 28 ++++++++++++++++++++++++++++ tsconfig.json | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 .gitignore create mode 100644 package.json create mode 100644 src/index.tsx create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..897cb9f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +package-lock.json +dist \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..5ce1c17 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "spritz", + "version": "0.0.1", + "description": "initalize zustand stores with react context", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "zustand", + "react", + "context", + "dependency-injection" + ], + "author": "", + "license": "MIT", + "devDependencies": { + "@types/react": "^18.0.26", + "react": "^18.2.0", + "zustand": "^4.1.5" + } +} diff --git a/src/index.tsx b/src/index.tsx new file mode 100644 index 0000000..cc7170e --- /dev/null +++ b/src/index.tsx @@ -0,0 +1,28 @@ +import React, { createContext as createReactContext, useContext, useRef } from 'react'; +import { StoreApi, useStore as useZustandStore } from 'zustand'; + +export const createContext = = StoreApi>() => { + const StoreContext = createReactContext(null); + const useStore = ( + selector: (state: State) => T = (state) => state as unknown as T, + equalityFn: (left: T, right: T) => boolean = Object.is + ) => { + const store = useContext(StoreContext); + if (!store) { + throw new Error('useProductListStore must be used within a ProductListProvider'); + } + return useZustandStore(store, selector, equalityFn); + }; + + const Provider: React.FC<{ createStore: () => Store; children?: React.ReactNode }> = ({ + createStore, + children, + }) => { + const store = useRef(createStore()).current; + return {children}; + }; + return { + Provider, + useStore, + }; +}; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..0505499 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,34 @@ +{ + "compilerOptions": { + "experimentalDecorators": true, + "baseUrl": ".", + "outDir": "build/dist", + "module": "ESNext", + "target": "ESNext", + "lib": ["es6", "dom", "esnext.asynciterable", "es2017"], + "sourceMap": true, + "allowJs": true, + "jsx": "react", + "moduleResolution": "node", + "rootDir": "src", + "forceConsistentCasingInFileNames": true, + "noImplicitReturns": true, + "noImplicitThis": true, + "noImplicitAny": true, + "strictNullChecks": true, + "suppressImplicitAnyIndexErrors": true, + "noUnusedLocals": true, + "skipLibCheck": true, + "allowSyntheticDefaultImports": true, + "removeComments": true + }, + "exclude": [ + "node_modules", + "build", + "scripts", + "acceptance-tests", + "webpack", + "jest", + "src/setupTests.ts" + ] +}