GithubHelp home page GithubHelp logo

Comments (5)

kiljag avatar kiljag commented on May 1, 2024 2

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.

PurpleMonkey729 avatar PurpleMonkey729 commented on May 1, 2024 1

@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.

MeenuyD avatar MeenuyD commented on May 1, 2024

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.

stephanemalho avatar stephanemalho commented on May 1, 2024

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.

stephanemalho avatar stephanemalho commented on May 1, 2024

@stephanemalho

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.