I want to obtain a value from a variable hosted in redux and memorize it, to later compare it with itself and verify if it is different.
I need, for example, to do the following : const value = useSelector(state => state.data.value); (suppose that here the value is 0) now, when value changes, i need to compare it with the value it had previously
2
Answers
You have to use selectors(i use ‘reselect’ library for that), such as:
file:
selectors.js
so then in your component you can use
mapStateToProps
withreselect
andconnect
this is not a full working example and the version that you might use, can have a bit different synaxis, but I hope this gives you a kick start. If not, let me know, I’ll add more extensive example
If you want to check what the value was on the previous render, you can save it in a ref:
If you’re doing this a lot you might find it useful to make a custom hook: