You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
export function useConstant<T>(init: () => T): T { const ref = useRef<T | null>(null) if (ref.current === null) { ref.current = init() } return ref.current }
This ensures the return of useConstant is constant. This means it's safe not to specify it in dep arrays, similar to how you don't need to specify the setState of useState. The various linters have options to skip errors in this case, but the React Compiler doesn't, as far as I can tell. It would nice to support a similar config in the compiler, so it correctly compiles components that make use of that return in any hook dependencies.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi.
We use the following hook in our codebase:
This ensures the return of
useConstant
is constant. This means it's safe not to specify it in dep arrays, similar to how you don't need to specify thesetState
ofuseState
.The various linters have options to skip errors in this case, but the React Compiler doesn't, as far as I can tell.
It would nice to support a similar config in the compiler, so it correctly compiles components that make use of that return in any hook dependencies.
Beta Was this translation helpful? Give feedback.
All reactions