如果你确定你永远不需要独立读取或写入电子邮件和电话状态,一个简单的方法是使用带有对象值的单个原子(这相当于使用 React 的 useState 钩子和对象值):
import {atom} from 'recoil';
const contactInfoState = atom({
key: 'contactInfo',
default: {
email: '',
phone: '',
},
});
然后,像这样使用(每次更新整个对象):
import {useRecoilState} from 'recoil';
function Registration () {
const [{email, phone}, setContactInfo] = useRecoilState(contactInfoState);
return (
<form>
<input
type="text"
value={email}
onChange={ev => setContactInfo({email: ev.target.value, phone})}
placeholder="Email Address"
/>
<input
type="text"
value={phone}
onChange={ev => setContactInfo({email, phone: ev.target.value})}
placeholder="Phone Number"
/>
</form>
)
}
但是,执行此操作的惯用方法(以及 Recoil 变得更强大的地方)是使用 selector 组合 atoms,它可以提供一起读取和写入值的方法(就像在示例中一样以上),但仍然允许使用它们的原子独立地读取和写入它们:
import {atom, DefaultValue, selector} from 'recoil';
const emailState = atom({
key: 'email',
default: '',
});
const phoneState = atom({
key: 'phone',
default: '',
});
const contactInfoState = selector({
key: 'contactInfo',
get: ({get}) => {
// get values from individual atoms:
const email = get(emailState);
const phone = get(phoneState);
// then combine into desired shape (object) and return:
return {email, phone};
},
set: ({set}, value) => {
// in a Reset action, the value will be DefaultValue (read more in selector docs):
if (value instanceof DefaultValue) {
set(emailState, value);
set(phoneState, value);
return;
}
// otherwise, update individual atoms from new object state:
set(emailState, value.email);
set(phoneState, value.phone);
},
});
这是一个完整且自包含的 sn-p 示例,您可以在此页面上运行它来验证它是否有效:
注意:它使用 React、ReactDOM 和 Recoil 的 UMD 版本,因此使用这些名称而不是使用 import 语句来全局公开它们。
<script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/recoil@0.5.2/umd/recoil.min.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.16.3/babel.min.js"></script>
<div id="root"></div>
<script type="text/babel" data-type="module" data-presets="react">
const {
atom,
DefaultValue,
RecoilRoot,
selector,
useRecoilValue,
useSetRecoilState,
} = Recoil;
const emailState = atom({
key: 'email',
default: '',
});
const phoneState = atom({
key: 'phone',
default: '',
});
const contactInfoState = selector({
key: 'contactInfo',
get: ({get}) => {
const email = get(emailState);
const phone = get(phoneState);
return {email, phone};
},
set: ({set}, value) => {
if (value instanceof DefaultValue) {
set(emailState, value);
set(phoneState, value);
return;
}
set(emailState, value.email);
set(phoneState, value.phone);
},
});
function Registration () {
const {email, phone} = useRecoilValue(contactInfoState);
const setEmail = useSetRecoilState(emailState);
const setPhone = useSetRecoilState(phoneState);
return (
<form>
<input
type="text"
value={email}
onChange={ev => setEmail(ev.target.value)}
placeholder="Email Address"
/>
<input
type="text"
value={phone}
onChange={ev => setPhone(ev.target.value)}
placeholder="Phone Number"
/>
</form>
)
}
function DisplayState () {
const email = useRecoilValue(emailState);
const phone = useRecoilValue(phoneState);
return (
<pre>
<code>{JSON.stringify({email, phone}, null, 2)}</code>
</pre>
);
}
function Example () {
return (
<RecoilRoot>
<Registration />
<DisplayState />
</RecoilRoot>
);
}
ReactDOM.render(<Example />, document.getElementById('root'));
</script>