pmndrs / valtio

💊 Valtio makes proxy-state simple for React and Vanilla
http://valtio.pmnd.rs
MIT License
8.67k stars 241 forks source link

Support option to disable render optimization in useSnapshot #846

Closed 98mux closed 5 months ago

98mux commented 5 months ago

I have an app where the image can update without the image_url updating, this caused an issue with useSnapshot and it would not register a change as image_url was identical to the previous one. So setting image_url would not update the image

A solution for this could be to add an option to useSnapshot, like this: useSnapshot(state, {disableOptimization:true});

Currently the solution I'm using is this:

const image_url = state.image_url:
state.image_url = undefined;
setTimeout(() => {state.image_url = image_url}, 10);

thought it is very ugly and causes 10ms of "flashing"

Maybe there is another way to solve this better?

dai-shi commented 5 months ago

Thanks for your feedback. It's still vague, but I will probably consider changing the useSnapshot behavior in v3 and such options can be considerable. (but, as v2 isn't released yet, it's too early to consider.)

For now, I'd suggest to try use-valtio as an escape hatch.