Thanks for your patience with my many questions. We're really enjoying working with Recoil and figuring out what our approaches will be.
Is it best practice to do this:
export const MyStore = atom({
key: "storeState",
default:{
foo: "",
bar: [],
baz: 0
},
});
With selectors to access/change the individual properties?
Or this?
export const FooState = atom({
key: "fooState",
default: ""
});
export const BarState = atom({
key: "barState",
default: []
});
export const BazState = atom({
key: "bazState",
default: 0
});
Either approach works. From our experience we have found best-practice to use separate atoms with primitive or simpler values:
But, there are cases where it makes sense for more compound atoms, such as when the properties are related and you want to enforce atomicity of multiple updates.
Thanks. Our pattern has been to only use selectors to get at atom properties. Is that as performant as using atoms, or does it cause re-renders we don't need?
It can currently lead to extra renders, as updating an atom will cause all dependent selectors and components to re-render, even if they only look at a part of the atom. We may optimize that (#314) in the future.
Most helpful comment
Either approach works. From our experience we have found best-practice to use separate atoms with primitive or simpler values:
But, there are cases where it makes sense for more compound atoms, such as when the properties are related and you want to enforce atomicity of multiple updates.