Skip to content

Commit

Permalink
updated example
Browse files Browse the repository at this point in the history
  • Loading branch information
shahriar-shojib committed Feb 26, 2022
1 parent 194b915 commit a41df47
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 72 deletions.
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './App.css';
import logo from './logo.svg';
import { counterStore, useCounterStoreSelector } from './store/counterStore';
import { counterStore, useCounterStoreSelector } from './stores/counterStore';

function App() {
const count = useCounterStoreSelector('count');
Expand Down
6 changes: 0 additions & 6 deletions example/src/store2/src/createSelector.ts

This file was deleted.

6 changes: 0 additions & 6 deletions example/src/store2/src/createStoreHooks.ts

This file was deleted.

4 changes: 0 additions & 4 deletions example/src/store2/src/index.ts

This file was deleted.

16 changes: 0 additions & 16 deletions example/src/store2/src/useStore.ts

This file was deleted.

28 changes: 0 additions & 28 deletions example/src/store2/src/useStoreSelector.ts

This file was deleted.

14 changes: 14 additions & 0 deletions example/src/stores/counterStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createStore } from '@poly-state/poly-state';
import { createStoreHooks } from '@poly-state/react';

type CounterStore = {
count: number;
};

const counterStoreInitialState: CounterStore = {
count: 0,
};

export const counterStore = createStore(counterStoreInitialState);

export const [useCounterStore, useCounterStoreSelector] = createStoreHooks(counterStore);
15 changes: 4 additions & 11 deletions src/useStoreSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ export const useStoreSelector = <T extends StateConstraint, U extends keyof T>(
const subscriberRef = useRef<() => void>();

useEffect(() => {
//clean up previous listeners if dependencies change
if (subscriberRef.current) {
subscriberRef.current();
}

//clean up previous listener if dependencies change
subscriberRef.current?.();
subscriberRef.current = store.subscribeKey(key as keyof T, setState as any);

return () => {
if (subscriberRef?.current) {
subscriberRef.current();
}
};
}, [store, key]);

useEffect(() => () => subscriberRef.current?.());

return state;
};

0 comments on commit a41df47

Please sign in to comment.