Comparing version 0.5.0 to 0.5.1
@@ -443,4 +443,8 @@ 'use strict'; | ||
var keyCount = 0; // global key count for all atoms | ||
function atom(read, write) { | ||
var config = {}; | ||
var config = { | ||
key: ++keyCount | ||
}; | ||
@@ -447,0 +451,0 @@ if (typeof read === 'function') { |
@@ -439,4 +439,8 @@ var jotai = (function (exports, react, useContextSelector) { | ||
var keyCount = 0; // global key count for all atoms | ||
function atom(read, write) { | ||
var config = {}; | ||
var config = { | ||
key: ++keyCount | ||
}; | ||
@@ -443,0 +447,0 @@ if (typeof read === 'function') { |
@@ -406,4 +406,8 @@ import { useState, useRef, useEffect, useMemo, createElement, useCallback, useDebugValue, useLayoutEffect } from 'react'; | ||
let keyCount = 0; // global key count for all atoms | ||
function atom(read, write) { | ||
const config = {}; | ||
const config = { | ||
key: ++keyCount | ||
}; | ||
@@ -410,0 +414,0 @@ if (typeof read === 'function') { |
{ | ||
"name": "jotai", | ||
"private": false, | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "👻 Next gen state management that will spook you", | ||
@@ -6,0 +6,0 @@ "main": "index.cjs.js", |
@@ -170,3 +170,4 @@ <p align="center"> | ||
We will be organizing some more information later. Meanwhile, please see WIP materials in the issues. | ||
- [API Doc](https://github.com/react-spring/jotai/issues/27) | ||
- [Utils Doc](./docs/utils.md) | ||
- [Example code snippets](https://github.com/react-spring/jotai/labels/has%20snippet) | ||
- [API Doc draft](https://github.com/react-spring/jotai/issues/27) |
@@ -7,2 +7,3 @@ export declare type NonPromise<T> = T extends Promise<unknown> ? never : T; | ||
export declare type Atom<Value> = { | ||
key: unknown; | ||
init?: Value; | ||
@@ -9,0 +10,0 @@ read: (get: Getter) => Value | Promise<Value>; |
import { WritableAtom } from 'jotai'; | ||
import type { NonPromise, NonFunction, SetStateAction, PrimitiveAtom } from './types'; | ||
export declare const useUpdateAtom: <Value, Update>(anAtom: WritableAtom<Value, Update>) => (update: Update) => void; | ||
declare const RESET: unique symbol; | ||
export declare const atomWithReset: <Value>(initialValue: NonFunction<NonPromise<Value>>) => WritableAtom<Value, typeof RESET | NonFunction<Value> | ((prev: Value) => NonFunction<Value>)>; | ||
export declare const useResetAtom: <Value>(anAtom: WritableAtom<Value, typeof RESET>) => (update: unknown) => void; | ||
export declare const useReducerAtom: <Value, Action>(anAtom: WritableAtom<Value, SetStateAction<Value>>, reducer: (v: Value, a: Action) => NonFunction<Value>) => readonly [NonPromise<Value>, (action: Action) => void]; | ||
export declare const atomWithReducer: <Value, Action>(initialValue: NonFunction<NonPromise<Value>>, reducer: (v: Value, a: Action) => Value) => WritableAtom<Value, Action>; | ||
export {}; |
30
utils.js
@@ -1,2 +0,2 @@ | ||
import { useMemo } from 'react'; | ||
import { useMemo, useCallback } from 'react'; | ||
import { atom, useAtom } from 'jotai'; | ||
@@ -8,3 +8,29 @@ | ||
}; | ||
const RESET = Symbol(); | ||
const atomWithReset = initialValue => { | ||
const anAtom = atom(initialValue, (get, set, update) => { | ||
if (update === RESET) { | ||
set(anAtom, initialValue); | ||
} else { | ||
set(anAtom, typeof update === 'function' ? update(get(anAtom)) : update); | ||
} | ||
}); | ||
return anAtom; | ||
}; | ||
const useResetAtom = anAtom => { | ||
const writeOnlyAtom = useMemo(() => atom(null, (_get, set, _update) => set(anAtom, RESET)), [anAtom]); | ||
return useAtom(writeOnlyAtom)[1]; | ||
}; | ||
const useReducerAtom = (anAtom, reducer) => { | ||
const [state, setState] = useAtom(anAtom); | ||
const dispatch = useCallback(action => { | ||
setState(prev => reducer(prev, action)); | ||
}, [setState, reducer]); | ||
return [state, dispatch]; | ||
}; | ||
const atomWithReducer = (initialValue, reducer) => { | ||
const anAtom = atom(initialValue, (get, set, action) => set(anAtom, reducer(get(anAtom), action))); | ||
return anAtom; | ||
}; | ||
export { useUpdateAtom }; | ||
export { atomWithReducer, atomWithReset, useReducerAtom, useResetAtom, useUpdateAtom }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57759
1415
173