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

Refactor react imports #8

Merged
merged 2 commits into from
Feb 14, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/heavy-ravens-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'jotai-x': patch
---

Fix React imports for SSR
4 changes: 2 additions & 2 deletions packages/jotai-x/src/atomProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentProps } from 'react';
import React from 'react';
import { Provider } from 'jotai';

export type AtomProviderProps = ComponentProps<typeof Provider>;
export type AtomProviderProps = React.ComponentProps<typeof Provider>;

export { Provider as AtomProvider } from 'jotai';
26 changes: 11 additions & 15 deletions packages/jotai-x/src/createAtomProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import React, {
createContext,
FC,
useContext,
useEffect,
useMemo,
useState,
} from 'react';
import React from 'react';
import { createStore } from 'jotai/vanilla';

import { AtomProvider, AtomProviderProps } from './atomProvider';
Expand All @@ -21,7 +14,9 @@ const getFullyQualifiedScope = (storeName: string, scope: string) => {
* to reference any provider belonging to the store, regardless of scope.
*/
const PROVIDER_SCOPE = 'provider';
const AtomStoreContext = createContext<Map<string, JotaiStore>>(new Map());
const AtomStoreContext = React.createContext<Map<string, JotaiStore>>(
new Map()
);

/**
* Tries to find a store in each of the following places, in order:
Expand All @@ -34,7 +29,7 @@ export const useAtomStore = (
scope: string = PROVIDER_SCOPE,
warnIfUndefined: boolean = true
): JotaiStore | undefined => {
const storeContext = useContext(AtomStoreContext);
const storeContext = React.useContext(AtomStoreContext);
const store =
storeContext.get(getFullyQualifiedScope(storeName, scope)) ??
storeContext.get(getFullyQualifiedScope(storeName, PROVIDER_SCOPE));
Expand Down Expand Up @@ -82,23 +77,24 @@ export const HydrateAtoms = <T extends object>({
export const createAtomProvider = <T extends object, N extends string = ''>(
storeScope: N,
atoms: SimpleWritableAtomRecord<T>,
options: { effect?: FC } = {}
options: { effect?: React.FC } = {}
) => {
const Effect = options.effect;

// eslint-disable-next-line react/display-name
return ({ store, scope, children, resetKey, ...props }: ProviderProps<T>) => {
const [storeState, setStoreState] = useState<JotaiStore>(createStore());
const [storeState, setStoreState] =
React.useState<JotaiStore>(createStore());

useEffect(() => {
React.useEffect(() => {
if (resetKey) {
setStoreState(createStore());
}
}, [resetKey]);

const previousStoreContext = useContext(AtomStoreContext);
const previousStoreContext = React.useContext(AtomStoreContext);

const storeContext = useMemo(() => {
const storeContext = React.useMemo(() => {
const newStoreContext = new Map(previousStoreContext);

if (scope) {
Expand Down
18 changes: 11 additions & 7 deletions packages/jotai-x/src/createAtomStore.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '@testing-library/jest-dom';

import React, { ReactNode, useState } from 'react';
import React from 'react';
import { act, queryByText, render, renderHook } from '@testing-library/react';
import { atom, PrimitiveAtom, useAtomValue } from 'jotai';
import { splitAtom } from 'jotai/utils';
Expand Down Expand Up @@ -54,8 +54,8 @@ describe('createAtomStore', () => {
const MUTABLE_PROVIDER_INITIAL_AGE = 19;
const MUTABLE_PROVIDER_NEW_AGE = 20;

const MutableProvider = ({ children }: { children: ReactNode }) => {
const [age, setAge] = useState(MUTABLE_PROVIDER_INITIAL_AGE);
const MutableProvider = ({ children }: { children: React.ReactNode }) => {
const [age, setAge] = React.useState(MUTABLE_PROVIDER_INITIAL_AGE);

return (
<>
Expand All @@ -71,8 +71,12 @@ describe('createAtomStore', () => {
);
};

const BecomeFriendsProvider = ({ children }: { children: ReactNode }) => {
const [becameFriends, setBecameFriends] = useState(false);
const BecomeFriendsProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
const [becameFriends, setBecameFriends] = React.useState(false);

return (
<>
Expand Down Expand Up @@ -107,7 +111,7 @@ describe('createAtomStore', () => {

const BecomeFriendsSetter = () => {
const setBecomeFriends = useMyTestStoreStore().set.becomeFriends();
const [becameFriends, setBecameFriends] = useState(false);
const [becameFriends, setBecameFriends] = React.useState(false);

return (
<>
Expand All @@ -125,7 +129,7 @@ describe('createAtomStore', () => {

const BecomeFriendsUser = () => {
const [, setBecomeFriends] = useMyTestStoreStore().use.becomeFriends();
const [becameFriends, setBecameFriends] = useState(false);
const [becameFriends, setBecameFriends] = React.useState(false);

return (
<>
Expand Down
16 changes: 9 additions & 7 deletions packages/jotai-x/src/createAtomStore.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
import { useHydrateAtoms } from 'jotai/utils';

import { atomWithFn } from './atomWithFn';
import { createAtomProvider, useAtomStore } from './createAtomProvider';

import type { ProviderProps } from './createAtomProvider';
import type { FC } from 'react';
import type { Atom, createStore, WritableAtom } from 'jotai/vanilla';

export type JotaiStore = ReturnType<typeof createStore>;
Expand Down Expand Up @@ -114,7 +114,7 @@ export type AtomStoreApi<
> = {
name: N;
} & {
[key in keyof Record<NameProvider<N>, object>]: FC<
[key in keyof Record<NameProvider<N>, object>]: React.FC<
ProviderProps<StoreInitialValues<T>>
>;
} & {
Expand Down Expand Up @@ -164,7 +164,7 @@ export interface CreateAtomStoreOptions<
> {
name: N;
delay?: UseAtomOptions['delay'];
effect?: FC;
effect?: React.FC;
extend?: (atomsWithoutExtend: StoreAtomsWithoutExtend<T>) => E;
}

Expand Down Expand Up @@ -287,10 +287,12 @@ export const createAtomStore = <
}
}

const Provider: FC<ProviderProps<MyStoreInitialValues>> = createAtomProvider<
MyStoreInitialValues,
N
>(name, writableAtomsWithoutExtend, { effect });
const Provider: React.FC<ProviderProps<MyStoreInitialValues>> =
createAtomProvider<MyStoreInitialValues, N>(
name,
writableAtomsWithoutExtend,
{ effect }
);

const storeApi: StoreApi<T, E, N> = {
atom: atoms,
Expand Down
14 changes: 7 additions & 7 deletions packages/jotai-x/src/elementAtom.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '@testing-library/jest-dom';

import React, { ReactNode, useMemo, useState } from 'react';
import React from 'react';
import { act, render } from '@testing-library/react';

import { createAtomStore } from './createAtomStore';
Expand Down Expand Up @@ -63,9 +63,9 @@ describe('ElementProvider', () => {
children,
}: {
name: string;
children: ReactNode;
children: React.ReactNode;
}) => {
const element = useMemo(() => makeNameElement(name), [name]);
const element = React.useMemo(() => makeNameElement(name), [name]);

return (
<ElementProvider element={element} scope="name">
Expand All @@ -79,9 +79,9 @@ describe('ElementProvider', () => {
children,
}: {
age: number;
children: ReactNode;
children: React.ReactNode;
}) => {
const element = useMemo(() => makeAgeElement(age), [age]);
const element = React.useMemo(() => makeAgeElement(age), [age]);

return (
<ElementProvider element={element} scope="age">
Expand All @@ -99,9 +99,9 @@ describe('ElementProvider', () => {
initialAge: number;
increment: number;
buttonLabel: string;
children: ReactNode;
children: React.ReactNode;
}) => {
const [age, setAge] = useState(initialAge);
const [age, setAge] = React.useState(initialAge);

return (
<AgeElementProvider age={age}>
Expand Down
4 changes: 2 additions & 2 deletions packages/jotai-x/src/useHydrateStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
import React from 'react';
import { useSetAtom } from 'jotai';
import { useHydrateAtoms } from 'jotai/utils';

Expand Down Expand Up @@ -43,7 +43,7 @@ export const useSyncStore = (

const set = useSetAtom(atom, { store });

useEffect(() => {
React.useEffect(() => {
if (value !== undefined && value !== null) {
set(value);
}
Expand Down
Loading