[Question] What's the purpose of useIsoLayoutEffect?
See original GitHub issueHello 👋
Sorry to bother, but I’m asking myself a question. What’s the purpose of useIsoLayoutEffect
?
I get it’s used to keep refs in sync, but shouldn’t this be possible directly in render?
const state = api.getState()
const stateRef = useRef(state)
const selectorRef = useRef(selector)
const equalityFnRef = useRef(equalityFn)
const erroredRef = useRef(false)
const currentSliceRef = useRef<StateSlice>()
if (currentSliceRef.current === undefined) {
currentSliceRef.current = selector(state)
}
let newStateSlice: StateSlice | undefined
let hasNewStateSlice = false
// The selector or equalityFn need to be called during the render phase if
// they change. We also want legitimate errors to be visible so we re-run
// them if they errored in the subscriber.
if (
stateRef.current !== state ||
selectorRef.current !== selector ||
equalityFnRef.current !== equalityFn ||
erroredRef.current
) {
// Using local variables to avoid mutations in the render phase.
newStateSlice = selector(state)
hasNewStateSlice = !equalityFn(
currentSliceRef.current as StateSlice,
newStateSlice
)
}
// Instead of this
- useIsoLayoutEffect(() => {
- if (hasNewStateSlice) {
- currentSliceRef.current = newStateSlice as StateSlice
- }
- stateRef.current = state
- selectorRef.current = selector
- equalityFnRef.current = equalityFn
- erroredRef.current = false
- })
// Shouldn't this work too (and be more suited for React Native, which doesn't have useLayoutEffect)?
+ if (hasNewStateSlice) {
+ currentSliceRef.current = newStateSlice as StateSlice
+ }
+ stateRef.current = state
+ selectorRef.current = selector
+ equalityFnRef.current = equalityFn
+ erroredRef.current = false
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
What is useLayoutEffect hook and when do you use it
useEffect hook is called after the screen is painted. Therefore mutating the DOM again immediately after the screen has been painted, will ...
Read more >When should you use the useLayoutEffect hook in React?
Although underused, React's useLayoutEffect hook provides a clean and well-fitted solution to some otherwise over-engineered solutions.
Read more >useEffect vs useLayoutEffect - Kent C. Dodds
So here are some rules for you to consider when deciding which React Hook to use. useEffect. 99% of the time this is...
Read more >React useLayoutEffect vs. useEffect with examples
In this tutorial, I'll describe the differences between useEffect and useLayoutEffect with concrete examples that'll help you cement your ...
Read more >What is useLayoutEffect Hook & how it compares to useEffect?
useEffect and useLayoutEffect shares similar function signatures, meaning the API exposed to the developers are similar in both cases. Given ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Well, there are a lot of discussions on this topic by someone and react team, in twitter and github. This gist might help: https://gist.github.com/sebmarkbage/75f0838967cd003cd7f9ab938eb1958f
I think you would agree that this is not pure.
So, this?
No problem!