admin管理员组文章数量:1332377
Can I set the default value of a Recoil atom to be an object?
e.g.:
export const currentUserState = atom({
key: 'currentUserState',
default: { name: '', email: '', userId: null },
});
And then set it with:
import { currentUserState } from '../atoms/atoms';
const setUserState = useSetRecoilState(currentUserState);
setUserState(name: 'John', email: '[email protected]', userId: getRand());
Can I set the default value of a Recoil atom to be an object?
e.g.:
export const currentUserState = atom({
key: 'currentUserState',
default: { name: '', email: '', userId: null },
});
And then set it with:
import { currentUserState } from '../atoms/atoms';
const setUserState = useSetRecoilState(currentUserState);
setUserState(name: 'John', email: '[email protected]', userId: getRand());
Share
Improve this question
edited Jul 24, 2021 at 20:47
Yoav Kadosh
5,1754 gold badges43 silver badges58 bronze badges
asked Oct 17, 2020 at 19:44
Kirk RossKirk Ross
7,16315 gold badges67 silver badges125 bronze badges
2 Answers
Reset to default 4Yes, It is allowed. Recoil atom state can be an object. You have initialized the atom correctly, but when you set the atom you have to pass an object since the state is object.
Initialize the currentUserState
atom
export const currentUserState = atom({
key: 'currentUserState',
default: {name: '', email: '', userId: null}
});
and then set the atom state as follows
import {currentUserState} from '../recoilstate/atoms';
const setUserState = useSetRecoilState(currentUserState);
setUserState({
name: 'John',
email: '[email protected]',
userId: getRand()
});
Yes, a Recoil atom can be an object.
I have written this code, which you can see below in the working demo.
const changeValue = () => {
setUserState({ name: "John", email: "[email protected]", userId: Math.random() });
};
Working Demo
本文标签: javascriptCan the default value of a Recoil atom be an objectStack Overflow
版权声明:本文标题:javascript - Can the default value of a Recoil atom be an object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742295432a2448629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论