Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change where storeCreator is passed #26

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@ type ExtractState<Store> = Store extends { getState: () => infer T }
export const createContext = <
State,
Store extends StoreApi<State> = StoreApi<State>
>() => {
>(createStore: () => Store) => {
const StoreContext = reactCreateContext<Store | undefined>(undefined);

type Provider = React.FC<
{
createStore: () => Store;
children: React.ReactNode;
} & Record<string, unknown>
>;

const Provider: Provider = ({ createStore, ...rest }) => {
const Provider: Provider = (props) => {
const storeRef = useRef<Store>();
return createElement(StoreContext.Provider, {
value: (storeRef.current ||= createStore()),
...rest,
...props,
});
};

Expand Down
30 changes: 20 additions & 10 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ type CounterState = {
};

it("creates and uses context store", async () => {
const { Provider, useStore } = createContext<CounterState>();

const createStore = () =>
create<CounterState>((set) => ({
create<CounterState>()((set) => ({
count: 0,
inc: () => set((state) => ({ count: state.count + 1 })),
}));
const { Provider, useStore } = createContext<CounterState>(createStore);

function Counter() {
const { count, inc } = useStore((state) => state);
Expand All @@ -39,7 +38,7 @@ it("creates and uses context store", async () => {

const { findByText } = render(
<>
<Provider createStore={createStore}>
<Provider>
<Counter />
</Provider>
</>
Expand All @@ -49,13 +48,12 @@ it("creates and uses context store", async () => {
});

it("uses context store with selectors", async () => {
const { Provider, useStore } = createContext<CounterState>();

const createStore = () =>
create<CounterState>((set) => ({
create<CounterState>()((set) => ({
count: 0,
inc: () => set((state) => ({ count: state.count + 1 })),
}));
const { Provider, useStore } = createContext<CounterState>(createStore);

function Counter() {
const count = useStore((state) => state.count);
Expand All @@ -66,7 +64,7 @@ it("uses context store with selectors", async () => {

const { findByText } = render(
<>
<Provider createStore={createStore}>
<Provider>
<Counter />
</Provider>
</>
Expand Down Expand Up @@ -94,7 +92,13 @@ it("throws error when not using provider", async () => {
}
}

const { useStore } = createContext<StoreApi<CounterState>>();
const createStore = () =>
create<CounterState>()((set) => ({
count: 0,
inc: () => set((state) => ({ count: state.count + 1 })),
}));

const { useStore } = createContext<CounterState>(createStore);
function Component() {
useStore((state) => state);
return <div>no error</div>;
Expand All @@ -111,7 +115,13 @@ it("throws error when not using provider", async () => {
});

it("useCallback with useStore infers types correctly", async () => {
const { useStore } = createContext<CounterState>();
const createStore = () =>
create<CounterState>()((set) => ({
count: 0,
inc: () => set((state) => ({ count: state.count + 1 })),
}));

const { useStore } = createContext<CounterState>(createStore);
function _Counter() {
const _x = useStore(useCallback((state) => state.count, []));
expectAreTypesEqual<typeof _x, number>().toBe(true);
Expand Down
Loading