From 3339e8c26849080ab761d06c232c91e6eb9b7d82 Mon Sep 17 00:00:00 2001 From: Alexandre Mouton-Brady Date: Sun, 20 Dec 2020 16:24:50 +0100 Subject: [PATCH] :sparkles: Adding props capability to createStore --- README.md | 36 +++++++++++++++++++++++++++++++++--- src/createStore.tsx | 17 ++++++++++++----- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1dfbfaa..c5f0eb9 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,9 @@ The ultimate companion of all your [solid-js](https://github.com/ryansolid/solid - [Basic usage](#basic-usage) - [With providers](#with-providers) - [With disposable app](#with-disposable-app) - - [createProvider](#createprovider) + - [createStore](#createstore) - [Basic usage](#basic-usage-1) + - [With props](#with-props) ## Features @@ -99,14 +100,14 @@ if (module.hot) { } ``` -### createProvider +### createStore A small utility that helps generate Provider & associated hook #### Basic usage ```tsx -const [Provider, useProvider] = createProvider( +const [Provider, useProvider] = createStore( { count: 0, first: 'Alexandre' }, (set, get) => ({ increment(by = 1) { @@ -129,4 +130,33 @@ const Counter = () => { } render(() => , document.getElementById('app')) +``` + +#### With props + +```tsx +const [Provider, useProvider] = createStore<{ count: number }>( + (props) => ({ count: props.count, first: 'Alexandre' }), + + (set, get) => ({ + increment(by = 1) { + set('count', count => count + 1) + }, + dynamicFullName(last) { + return `${get.first} ${last} ${get.count}`; + } + }) +) + +const Counter = () => { + const [state, { increment, dynamicFullName }] = useProvider() + + // The count here will be synced between the two because it's global + return <> + +

{dynamicFullName('Mouton-Brady')}

+ +} + +render(() => , document.getElementById('app')) ``` \ No newline at end of file diff --git a/src/createStore.tsx b/src/createStore.tsx index 3b0d414..3b0b153 100644 --- a/src/createStore.tsx +++ b/src/createStore.tsx @@ -11,8 +11,15 @@ interface CreateStoreFn }> { (store: A, fn: (set: SetStateFunction, get: State) => B): readonly [State, C]; } -function generateStore(store: A, fn: (set: SetStateFunction, get: State) => B) { - const [get, set] = createState(store); +type CommonObject = Record; + +function generateStore( + store: A, + fn: (set: SetStateFunction, get: State) => B, + props?: C, +) { + const finalStore: A = typeof store === 'function' ? store(props) : store; + const [get, set] = createState(finalStore); return [get, { ...fn(set, get), set }] as const; } @@ -45,15 +52,15 @@ function generateStore(store: A, fn: (set: SetStateFunction, get: State * app.mount('#app') * ``` */ -export function createStore, B>( +export function createStore

( store: T, fn: (set: SetStateFunction, get: State) => B, ) { type Store = ReturnType>; const Context = createContext(); - const Provider: Component = (props) => { - const value: Store = generateStore(store, fn); + const Provider: Component

= (props) => { + const value: Store = generateStore(store, fn, props); return {props.children}; };