State is mutable without calling store.setState
codesections opened this issue · 1 comments
My understanding is that one of the advantages of unistore/other state-management solutions is that state is immutable other than through calls to setState
. However, code such as the following will alter state:
const Component = ({ array }) => {
return (
<div>
{array.reverse().map((el) => (
<span>{el}</span>
))}
</div>
);
};
export default connect('array')(Component);
Is that intended behavior or a bug? If it is intended behavior, are there any patterns to ensure that my code does not mutate state other than through calls to setState()
?
@codesections this is by design - there is a cost associated with enforcing this that shouldn't be paid in production, and Unistore doesn't do development/production differentiation (because there isn't a great way to do it that works reliably in all bundles).
If you've got a nice way to do development-only code, you can use this middleware:
function makeStoreImmutable(store) {
store.subscribe(state => {
Object.freeze(state);
});
}
Could be a nice addition to the readme or wiki!