Replies: 4 comments 1 reply
-
I've tried such implementation import {list} from './store'
export function updateToDoList(newItem) {
const [value, setter] = useRecoilState(list)
setter([...value, newItem])
} or import { useRecoilTransaction_UNSTABLE as transition } from 'recoil';
export const updateToDoList = transition(
({ get, set }) =>
() => {
const oldTodoList = get(list);
set(list, [
...oldTodoList,
{ text: 'Buy a new house', complete: false },
]);
}
); Although it's said that
Sadly, such apis are still bound to a react environment with 'hook' name rules. |
Beta Was this translation helpful? Give feedback.
-
Let's say it follows the React rule |
Beta Was this translation helpful? Give feedback.
-
For actions, check out the reducer example for Note that for full proper support for React concepts such as concurrent mode, transitions, async rendering, etc. the actual Recoil state is coupled with the React rendering context. This is why it is is accessible through hooks when rendering. But, you can still organize and separate your action logic, if desired, just by wrapping it as a hook: export const useUpdateToDoList = () => useRecoilTransaction_UNSTABLE(
({ get, set }) =>
() => {
const oldTodoList = get(list);
set(list, [
...oldTodoList,
{ text: 'Buy a new house', complete: false },
]);
}
);
function MyComponent() {
const updateToDoList = useUpdateToDoList();
...
} Also, for simple updates you can just use the updater form of a setter by passing in a function: function MyComponent() {
const setToDoList = useSetRecoilState(listState);
return (
<div onClick={() => {
setToDoList(list => [...list, {text: 'New item', complete: false}]);
}} >Add item</div>
);
} |
Beta Was this translation helpful? Give feedback.
-
As claimed in the doc:
In a real production project. MVVM should be presented as
store
&action
&view
3 files.Say, take 'todo list' for example:
Beta Was this translation helpful? Give feedback.
All reactions