Comments (7)
Hi @catamphetamine ,
You're using strict mode, and useInsertionEffect
does not mount twice the way useLayoutEffect
and useEffect
do.
from react.
You're using strict mode, and
useInsertionEffect
does not mount twice the wayuseLayoutEffect
anduseEffect
do.
Thanks, but why doesn't it though?
Don't you consider it a bug?
They're all "effect"s, so why does their behavior differ in strict mode?
That makes no sense and useInsertionEffect()
isn't supposed to be anything "special":
useInsertionEffect
is a version of useEffect that fires before any DOM mutations.
From the official docs:
https://beta.reactjs.org/reference/react/useInsertionEffect
So I consider the current behavior a bug.
from react.
Looks like in the current versions of React, the only workaround for the "strict" mode would be modifying the effect hooks like this:
import { useRef, useCallback } from 'react'
// A workaround for a React bug when `useInsertionEffect()` doesn't run twice on mount
// in "strict" mode unlike `useEffect()` and `useLayoutEffect()` do.
// https://github.com/facebook/react/issues/26320
export default function useEffectDontMountTwiceInStrictMode(useEffect, handler, dependencies) {
if (!Array.isArray(dependencies)) {
throw new Error('Dependencies argument must be an array')
}
const { onEffect } = useEffectStatus()
const { onChange } = usePrevousValue(dependencies)
useEffect(() => {
const { isInitialRun } = onEffect()
const previousDependencies = onChange(dependencies)
if (isInitialRun || !isShallowEqualArrays(previousDependencies, dependencies)) {
const cleanUpFunction = handler()
if (typeof cleanUpFunction === 'function') {
throw new Error('An effect can\'t return a clean-up function when used with `useEffectDontMountTwiceInStrictMode()` because the clean-up function won\'t behave correctly in that case')
}
}
}, dependencies)
}
function useEffectStatus() {
const hasMounted = useRef(false)
const onEffect = useCallback(() => {
const wasAlreadyMounted = hasMounted.current
hasMounted.current = true
return {
isInitialRun: !wasAlreadyMounted
}
}, [])
return {
onEffect
}
}
function usePrevousValue(value) {
const prevValue = useRef(value)
const onChange = useCallback((value) => {
const previousValue = prevValue.current
prevValue.current = value
return previousValue
}, [])
return {
onChange
}
}
function isShallowEqualArrays(a, b) {
if (a.length !== b.length) {
return false
}
let i = 0
while (i < a.length) {
if (a[i] !== b[i]) {
return false
}
i++
}
return true
}
import { useLayoutEffect } from 'react'
function Component() {
useEffectDontMountTwiceInStrictMode(useLayoutEffect, handler, dependencies)
}
For example, in my case, a library uses useInsertionEffect()
/ useLayoutEffect()
to squeeze into that tiny timeframe in order to accept some calls from child components' useLayoutEffect()
s, so 1:1
calls correspondence is a requirement.
from react.
I think you are correct, it needs to mount twice. I am working on this bug now and the discussion is happening here #26332
from react.
@egonSchiele Wow, that was quick, thanks 👍
from react.
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
from react.
from react.
Related Issues (20)
- [DevTools Bug] Cannot add node "1" because a node with that id is already in the Store. HOT 1
- [React 19] When multiple components call `use` inside a `Suspense`, it crosses the boundary of `Suspense` and stops rendering of other `Suspense` as well HOT 2
- [Compiler]: Type inference information does not propagate past phi nodes HOT 1
- Bug: "Should not already be working" Error Provides No Information (Not Even the Name Indicates What's Wrong)
- [React 19] React-Dom bundle increase 200kb HOT 3
- Bug: Expected Static Flag Was Missing
- [React 19] useEffect does not re-fire on hot reload with React 19 and vite HOT 1
- Bug: Hundreds render of Suspense child with hydration error HOT 5
- Bug: Receiving `Error: Should have a queue. This is likely a bug in React. Please file an issue` on usage of `next-safe-action`'s `useAction` hook HOT 1
- [React 19] infinite loop when using React lazy & functional formAction together. HOT 4
- The const array variable value populated in a function does not reflect the updated values when called in another function in Firefox HOT 2
- Bug: DoS vulnerability in ws dependency
- [React 19] Inconsistent "cache" api with Async Operations in react-server HOT 2
- [Compiler Bug]: Increment expression in for-loop loses assignment operation HOT 4
- Bug: TypeError: Cannot read properties of null (reading 'useState')
- Ensures role attribute has an appropriate value for the element (aria-allowed-role - https://dequeuniversity.com/rules/axe/4.7/aria-allowed-role?application=msftAI) HOT 1
- Ensures ARIA attributes are allowed for an element's role (aria-allowed-attr - https://accessibilityinsights.io/info-examples/web/aria-allowed-attr)Bug: HOT 1
- Bug: HOT 1
- [VLG]¿Cómo hablar con Vueling por teléfono? @telefono españa HOT 1
- hook invalid
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.