ctrlplusb / easy-peasy

Vegetarian friendly state for React

Home Page:https://easy-peasy.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why are listeners evaluated by calling a thunk

mfbx9da4 opened this issue · comments

I'm not sure why listeners are evaluated by thunks since they can't modify state, they only read state.

https://codesandbox.io/s/listeners-evaluated-by-thunks-0h0p9

As seen in the screenshot below the listener is evaluated four times for one thunk + action. My expectation is that the listeners only need to be evaluated after an action completes

image

It appears to be a design of Redux. Internally the state has not changed. Your useStoreState mappers shouldn't be doing expensive compute. Merely mapping state which gets strict equality checked. If the state hasn't changed the operation costs next to nothing and your component is not rendered.

I've updated your sandbox to make this evident.

https://codesandbox.io/s/listeners-evaluated-by-thunks-forked-5w2t0?file=/src/App.js

Makes sense, I guess I just find it hard to come to terms with the react / redux ethos which is effectively "re render the entire app anytime anything changes and cache things where necessary". In my experience the the caching code adds unnecessary complexity to simple things. In demo apps things remain snappy but then death by a thousand cuts 🤷‍♂️

As always a trade off.