Comments (5)
That is the default behavior of react strict mode. react renders each component twice in development mode.
check out this. https://react.dev/reference/react/StrictMode
from react.
@stephanemalho
Thanks for your answer.
At 2th step "• than he go to the children component", 'than" is right?
Is it "then", isn't it?
from react.
Hello yes react in strict render the component twice and when using the useEffect hook with an empty dependency array ([]), the effect is meant to run only once, similar to the componentDidMount lifecycle method in class components.
The cleanup function specified in the effect will be invoked when the component is unmounted, regardless of whether there are dependencies or not. However, in the case of an empty dependency array, the effect will not be re-triggered on subsequent re-renders of the component. I think so this behavior is intentional and follows React's rules for managing effects.
from react.
here is the order for the render & re-render:
• APP read his component but bypass useEffect
• then he go to the children component
• he read the children file and bypass useEffect
• Now he read the useEffect inside the children
• he Do a re-render of the children (bypassing useEffect duh)
• return to App to read if there is a useEffect
• re-rend APP
But in my opinion, you don't need to use the return fonction inside useEffect in this case. You only need this return if you want to run something inside the component just before he will unmont.
import "./styles.css";
import { useEffect } from "react";
export default function App() {
console.log("first read")
return (
{/* thing to return */}
);
}
const User = (props) => {
console.log("second read")
useEffect(() => {
console.log("user effect fourth read", props);
return () => {
console.log("user cleanup fifth read or first read of all if the componentWillUnmount() ", props);
};
}, []);
console.log("thirth read")
return {/* User return */};
};
`
from react.
Thanks for your answer.
At 2th step "• than he go to the children component", 'than" is right?
Is it "then", isn't it?
Yes it is you right
from react.
Related Issues (20)
- [DevTools Bug]: Update highlights crash often HOT 1
- Bug: Cannot update a component (`HashRouter`) while rendering a different component *. HOT 3
- Proposal to add new ReactHooks. HOT 6
- [DevTools Bug]: Excessive memory usage, even when not in use (Firefox) HOT 2
- React developer tools HOT 2
- Bug: HOT 14
- Bug: Update hook called on initial render. HOT 3
- Bug: Inconsistent scroll position changes when reordering a list of items with react keys HOT 4
- Bug: React + Typescript - useReducer's initializer arg is required even though it shouldn't be HOT 1
- Add `ignoredHooks` to `react-hooks/exhaustive-deps` HOT 1
- Bug: Calling bundle file using browserify from react gives Syntax error: react_app_testing/src/HashBundle.js: Unexpected character '�' (1:0) HOT 2
- Improve alignment of the footer links [making them responsive] HOT 3
- Bug: Different word cannot be used in place of 'children' as prop HOT 6
- Are Effect Events really necessary? HOT 3
- Bug: useCallback do not respect dependencies HOT 4
- Bug: Suspense does not work in Server Components with Safari on macOS or iOS HOT 4
- Bug: React breaks HTMLInputElement.prototype.value interceptions HOT 1
- README Update
- Bug: Unable to catch errors In React 18.2.0 version HOT 3
- Bug: `useId` generates a new different ID on second render of Strict Mode before un-mount HOT 8
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.