When does Preact re-render function components? #2877
-
I am managing my app state using just hooks for now, similar to the approach described in State management with just React hooks. The app is small so re-rendering components too often isn't a big issue at the moment, but I'm curious about how I would go about avoiding unnecessary re-renders. For context, I have a "store" that's created like this: const StoreContext = createContext({
state: DEFAULT_STATE,
dispatch: (() => { }) as (action: ACTION) => void
})
export function createStore() {
const [state, dispatch] = useReducer(reducer, DEFAULT_STATE)
const store = useMemo(() => ({ state, dispatch }), [state, dispatch])
return store
}
export function useStore() {
return useContext(StoreContext)
} If I have a function component that calls the Is the DOM element re-rendered when:
Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey 👋 Interesting question, let's take the following scenario to complete your example: const Child = () => {
const [state] = useStore();
return (<p>{state.text}</p>
} Context will emit to this consumer every time anything in the object changes, however this does not mean that anything will change to this
So concluding, without a change to |
Beta Was this translation helpful? Give feedback.
Hey 👋
Interesting question, let's take the following scenario to complete your example:
Context will emit to this consumer every time anything in the object changes, however this does not mean that anything will change to this
<p>
tag. In this case we have two possible scenario's:So concluding, without a change to
state.text
no expensive operations to the DOM will happen.