mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51:37 +00:00
fix: call selector with atom state
This commit is contained in:
parent
0046af5598
commit
8a0d0977ca
2 changed files with 4 additions and 2 deletions
|
|
@ -13,7 +13,7 @@ local function useAtomState<State>(atom: Atom<State>, selector: ((State) -> Stat
|
||||||
local React = requireReact()
|
local React = requireReact()
|
||||||
|
|
||||||
local state, setState = React.useState(function()
|
local state, setState = React.useState(function()
|
||||||
return if selector then atom(selector) else atom()
|
return if selector then selector(atom()) else atom()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local latestSelector = React.useRef(selector)
|
local latestSelector = React.useRef(selector)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,9 @@ type Atom<T> = types.Atom<T>
|
||||||
export type SetState<T> = (T | ((T) -> T)) -> ()
|
export type SetState<T> = (T | ((T) -> T)) -> ()
|
||||||
|
|
||||||
local function useSetAtom<State>(atom: Atom<State>): SetState<State>
|
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)
|
atom(state)
|
||||||
end, { atom })
|
end, { atom })
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue