Skip to content

Commit

Permalink
fix(isolation): fix prop types (#15)
Browse files Browse the repository at this point in the history
* fix(isolation): fix prop types

* update CHANGELOG
  • Loading branch information
dai-shi authored Jan 30, 2024
1 parent 0ddc58a commit 20b4fd8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## [Unreleased]
### Changed
- fix(isolation): fix prop types #15

## [0.5.0] - 2024-01-26
### Changed
Expand Down
8 changes: 4 additions & 4 deletions examples/01_isolation/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Counter = () => {
);
};

const ScopedCounter = () => {
const MyCounter = () => {
const [count, setCount] = useMyAtom(countAtom);
return (
<div>
Expand All @@ -35,12 +35,12 @@ const App = () => {
<h1>First Provider</h1>
<MyProvider>
<Counter />
<ScopedCounter />
<MyCounter />
</MyProvider>
<h1>Second Provider</h1>
<MyProvider>
<MyProvider initialValues={[[countAtom, 1]]}>
<Counter />
<ScopedCounter />
<MyCounter />
</MyProvider>
</div>
);
Expand Down
9 changes: 5 additions & 4 deletions src/createIsolation.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createContext, useContext, useRef } from 'react';
import type { ReactNode } from 'react';
import { createStore } from 'jotai/vanilla';
import type { WritableAtom } from 'jotai/vanilla';
import {
useAtom as useAtomOrig,
useAtomValue as useAtomValueOrig,
Expand All @@ -9,25 +10,25 @@ import {
import { useHydrateAtoms } from 'jotai/react/utils';

type Store = ReturnType<typeof createStore>;
type InitialValues = Parameters<typeof useHydrateAtoms>[0];
type AnyWritableAtom = WritableAtom<unknown, any[], any>;

export function createIsolation() {
const StoreContext = createContext<Store | null>(null);

const Provider = ({
store,
initialValues = [] as unknown as InitialValues,
initialValues = [],
children,
}: {
store?: Store;
initialValues?: InitialValues;
initialValues?: Iterable<readonly [AnyWritableAtom, unknown]>;
children: ReactNode;
}) => {
const storeRef = useRef(store);
if (!storeRef.current) {
storeRef.current = createStore();
}
useHydrateAtoms(initialValues, { store: storeRef.current });
useHydrateAtoms(initialValues as any, { store: storeRef.current });
return (
<StoreContext.Provider value={storeRef.current}>
{children}
Expand Down

0 comments on commit 20b4fd8

Please sign in to comment.