question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] What's the purpose of useIsoLayoutEffect?

See original GitHub issue

Hello 👋

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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Mar 9, 2021

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


const ref = { current: 0 }
const func = () => {
  ++ref.current
  return ref.current
}

I think you would agree that this is not pure.

const Comp = () => {
  const ref = useRef(0)
  ++ref.current
  return <>{ref.current}</>
}

So, this?

0reactions
dai-shicommented, Mar 9, 2021

No problem!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found