fix: call selector with atom state

This commit is contained in:
Richard 2024-03-30 02:00:07 -07:00
parent 0046af5598
commit 8a0d0977ca
2 changed files with 4 additions and 2 deletions

View file

@ -13,7 +13,7 @@ local function useAtomState<State>(atom: Atom<State>, selector: ((State) -> Stat
local React = requireReact()
local state, setState = React.useState(function()
return if selector then atom(selector) else atom()
return if selector then selector(atom()) else atom()
end)
local latestSelector = React.useRef(selector)

View file

@ -6,7 +6,9 @@ type Atom<T> = types.Atom<T>
export type SetState<T> = (T | ((T) -> T)) -> ()
local function useSetAtom<State>(atom: Atom<State>): SetState<State>
return requireReact().useCallback(function(state: State | ((State) -> State)): ()
local React = requireReact()
return React.useCallback(function(state: State | ((State) -> State)): ()
atom(state)
end, { atom })
end