When action is dispatched, a === reference comparison of prev ft return and current will trigger resulting in rerender if different
Use multiple useSelectors(they will be batched)
Dont return an object, will always rerender, reselect or similar lib can help here
2nd arg is the equality function to be used
import { shallowEqual, useSelector } from'react-redux'// later, doesn't seem to work with [] and [] ??constselectedData=useSelector(selectorReturningObject, shallowEqual)
CreateSelector()
Great for when you need to return an object as can shortcircuit better
import React from"react";import { useDispatch, useSelector } from"react-redux";import { createSelector } from"reselect";//fine to define here if only one instance of AppconstselectNumOfDoneTodos=createSelector( (state) =>state.todos, (todos) =>todos.filter((todo) =>todo.completed).length);exportdefaultfunctionDone() {constnum=useSelector(selectNumOfDoneTodos);return <div> DID {num} </div>;}
If selector is used in multiple component instances and depends on the props, must make unique selector instance per component instance (see here)