React: It's confusing that refs are not attached by the time useMutationEffect() runs

Created on 2 Nov 2018  路  6Comments  路  Source: facebook/react

This doesn't work but I'd expect it to.

https://codesandbox.io/s/x00vn4ylp

Hooks

All 6 comments

@gaearon Could it be that useMutationEffect() is being called synchronously as the DOM mutation, so there is no DOM element to even add a ref to?

I believe you would have to use useLayoutEffect() in this case, as it fires synchronously after all DOM mutations. Allowing the ref to "settle"

@Caryyon

I believe you would have to use useLayoutEffect() in this case, as it fires synchronously after all DOM mutations. Allowing the ref to "settle"

Ya it works @gaearon

What's the use of useMutationEffect() if no refs are set by the time it runs? I'm struggling to think of what it could be used for if you don't have any live handles to the DOM within its callback.

I also just noticed this, and that useLayoutEffect indeed lets you get a working reference.

Now that useMutationEffect has been banished to /dev/null this is possibly "fixed"?

Was this page helpful?
0 / 5 - 0 ratings