In easy-peasy, the useStoreState()
hook does not cause a re-render when we use the hook to access the store's field that stores an ES6 class instance. For example:
store.js
import { action, createStore } from "easy-peasy";
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const store = createStore({
person: new Person("Tom", "20"), // <-- Stores ES6 class instance
updatePersonName: action((state, payload) => {
state.person.name = payload;
})
});
export default store;
app.jsx
:
import "./styles.css";
import { useStoreActions, useStoreState } from "easy-peasy";
import React from "react";
export default function App() {
const person = useStoreState((state) => state.person);
return (
<div className="App">
<p>{JSON.stringify(person)}</p>
<EditPerson />
</div>
);
}
function EditPerson() {
const person = useStoreState((state) => state.person);
const updatePersonName = useStoreActions(
(actions) => actions.updatePersonName
);
return (
<input
value={person.name}
onChange={(e) => updatePersonName(e.target.value)}
/>
);
}
If we try to type in the input box, even though the updatePersonName
action is successfully dispatched (see the screenshot below), the value of the input box remains unchanged. The person
store state is not successfully updated and the useStoreState()
hook does not cause a re-render.