React Component Lifecycle - Hooks / Methods Explained

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ธ.ค. 2024

ความคิดเห็น • 115

  • @freecodecamp
    @freecodecamp  6 ปีที่แล้ว +4

    Check out our React playlist for more React tutorials: th-cam.com/play/PLWKjhJtqVAbkArDMazoARtNz1aMwNWmvC.html

  • @Gigatless
    @Gigatless 4 ปีที่แล้ว +18

    It's great to see someone who actually used a thing so they can explain the things you would actually need in real world.

  • @TricoliciSerghei
    @TricoliciSerghei 6 ปีที่แล้ว +28

    Masterpiece example of component lifecycle! Great explanation!

  • @fytubevw
    @fytubevw 4 ปีที่แล้ว +4

    Super clarity. Very calm and concise style, which makes the video easy to ingest.

    • @waltervenditti3174
      @waltervenditti3174 2 ปีที่แล้ว

      Yeah seriously. So sick of all the "wacky" programming videos filled with terrible jokes.

  • @sarvarkhalimov111
    @sarvarkhalimov111 3 ปีที่แล้ว +2

    Amazing tutorial to grasp concepts and where/how to use lifecycle methods. Thanks for this tutorial!

  • @PraveenKumar-lp6il
    @PraveenKumar-lp6il 3 ปีที่แล้ว +8

    add props as param in error component..otherwise compile time issue will happen in new version React

  • @michaelhashimoto1650
    @michaelhashimoto1650 ปีที่แล้ว +1

    I like class components more than functional components. It feels like you have more control.

  • @harunguven8581
    @harunguven8581 3 ปีที่แล้ว +11

    I come from The Odin Project and find this video useful! Especially for beginners.
    By the way how is your Odin Project adventure going? You guys must come very close to end of Javascript course. I

    • @byslyrten
      @byslyrten 3 ปีที่แล้ว +5

      Yes, it looks like we are getting kind of intermediate-advanced topics right now and I am really grateful to people who contribute this great curriculum that gets me from absolute beginner level to be able to create somewhat good level web apps.

    • @Fooljuice
      @Fooljuice 3 ปีที่แล้ว +7

      Good to see another Odin Project member here! We're almost at the end, let us finish strong!

    • @adarshtiwari7395
      @adarshtiwari7395 2 ปีที่แล้ว +2

      Hey fellow odin warrior😃

    • @othmaneelkantaoui9674
      @othmaneelkantaoui9674 2 ปีที่แล้ว +1

      i wish the odin project gave us more videos like this instead of lengthy articles

  • @illb2fast4u
    @illb2fast4u 6 ปีที่แล้ว +5

    Dude, this is so cool. I'm only learning JavaScript now. Just finished Vanilla JS, next up React.

    • @clashwithcmad
      @clashwithcmad 3 ปีที่แล้ว +1

      It's been 3 year What going on?

    • @martinwest7250
      @martinwest7250 2 ปีที่แล้ว

      you finished vanilla js???

    • @Sandeep-zu7gd
      @Sandeep-zu7gd ปีที่แล้ว +1

      almost 5 years are you a react master senpai now?

  • @leonzchang8790
    @leonzchang8790 3 ปีที่แล้ว +5

    thanks for the awesome video.
    I use create-react-app to run the code ,wondering why componentDidCatch won't show parent component but showing error

    • @lennygith6254
      @lennygith6254 3 ปีที่แล้ว +2

      I also did that and got similar results. If you can please update the comment when you find an error. I'll do the same too.

    • @DrDemolition
      @DrDemolition 3 ปีที่แล้ว +1

      ​@@lennygith6254 It's an overlay, you can press the x on top right and get the actual render.

  • @ristolibera1295
    @ristolibera1295 3 ปีที่แล้ว +1

    What an excellent tutorial!

  • @abhilashpoojary3812
    @abhilashpoojary3812 4 ปีที่แล้ว +1

    Great examples with neat explanation 😍

  • @NotTheLastOne
    @NotTheLastOne 6 ปีที่แล้ว +2

    18:43 you dont have the variable error in your state but you referring to this variable and it seems to be working. how does it work?
    it seems my react is not the same with your react thought I have the latest version

    • @nickkarnik9688
      @nickkarnik9688 4 ปีที่แล้ว

      TheLastOne thanks for pointing that out. No idea how I missed that! It works because it is treated as any other method. It isn’t called.

  • @danieldejager1847
    @danieldejager1847 2 ปีที่แล้ว

    Excellent tutorial , its pragmatic and to the point. Thank you.

  • @anant1995rawat1995
    @anant1995rawat1995 3 ปีที่แล้ว

    Amazing Bro!!!!!!. Best explaination
    ever

  • @VigneshSharma-vrs
    @VigneshSharma-vrs 6 ปีที่แล้ว +5

    11:50 while explaining shouldComponentUpdate, if the currentProp which is this.props.ignoreProp is not same as nextProps meaning that the prop has changed, we request for render and update. I'm little confused here, cos your code does the opposite of what I'm thinking.
    Please clarify.

    • @voyageruk2002
      @voyageruk2002 6 ปีที่แล้ว +1

      I think it should not update if the props are === rather than !==

    • @deepakbisht6215
      @deepakbisht6215 5 ปีที่แล้ว

      bro because the props have been updated
      the ignoreProp button have changed the state ignoreProp and hence the prop for the counter component using Math.random() function it would not update the componet ie return false
      ps - if it would be a case when math.random() generates same number twice then shouldComponentUpdate would have returned true and updated the component
      hope you understand

  • @reynaoldman9100
    @reynaoldman9100 3 ปีที่แล้ว

    Thanks, this video was very helpful for me.

  • @ANANDSHARMA-ws2xf
    @ANANDSHARMA-ws2xf 4 ปีที่แล้ว

    You explained amazing ways 👍

  • @mohamedreejan8143
    @mohamedreejan8143 4 ปีที่แล้ว

    Clear explanations,, very useful.. thanku👍

  • @anandhegde9345
    @anandhegde9345 5 ปีที่แล้ว +1

    if getDerivedStateFromProps is used for setting the state from props why can't it be done in constructor? We have access to props in constructor as well.

    • @nickkarnik9688
      @nickkarnik9688 4 ปีที่แล้ว +4

      Anand Hegde that will work only during object creation. If props were to change after that then the getDerivedStateFromProps hook is useful.

  • @adrielamadi8585
    @adrielamadi8585 3 ปีที่แล้ว

    im actually watching this now even though it was uploaded 3 years ago.. still very useful

  • @RafaelSFTech
    @RafaelSFTech 6 ปีที่แล้ว +7

    Is a good pratice using methods in constructor? i think is not

    • @jaredgiangrasso5965
      @jaredgiangrasso5965 5 ปีที่แล้ว +4

      I think he's defining his methods in the constructor to avoid having to bind them to 'this' in the constructor. Although I'm not sure why he doesn't just use arrow functions outside of the constructor instead which should accomplish the same thing.

  • @somasundaramb2281
    @somasundaramb2281 3 ปีที่แล้ว +1

    Hi,
    I have a doubt on ShouldComponentUpdate method implementation. That is, if present value and future value are different , then it should re-render.right?
    But,here it is not re-render
    Can any guide me

    • @adrielamadi8585
      @adrielamadi8585 3 ปีที่แล้ว

      exactly, it should only re-render or set to true if the present value and the future value are different and not when they are same

  • @karangupta725
    @karangupta725 4 ปีที่แล้ว

    why we want to render even if ignore prop is same as before at around 10:59?

    • @sagemode37
      @sagemode37 4 ปีที่แล้ว

      We are not rendering if it is same. return false if the prop is same so it won't render

  • @karangupta725
    @karangupta725 4 ปีที่แล้ว

    at 13:02 why did he used parseInt , doesn't Math.random() returns integer value?

    • @sagemode37
      @sagemode37 4 ปีที่แล้ว +1

      Math.random() returns random floating point value between 0 and 1 , so we can get anything , for eg: 0.67589432 . If you multiply this by 100 --> 67.589432 . And parseInt(67.589432) will return 67 .

  • @viditsharma3929
    @viditsharma3929 4 ปีที่แล้ว +1

    is it not better to use setState by passing it as a function and recording the previous value?

    • @matrixspielt
      @matrixspielt 3 ปีที่แล้ว

      Yup. Otherwise, the state could have already changed. For anyone who is wondering: this.setState(function(prevState, prevProps) {}).

  • @sulistyoardani7314
    @sulistyoardani7314 4 ปีที่แล้ว +1

    where ignoreCase comes from? minute 09:02

  • @someone-on-earth
    @someone-on-earth 7 หลายเดือนก่อน +1

    the functional approach with useEffect() and useState() would be easier imh

  • @karangupta725
    @karangupta725 4 ปีที่แล้ว +2

    is somebody even watching this old tutorial, other than me?

  • @m.a6141
    @m.a6141 2 ปีที่แล้ว

    Perfect Tutorial

  • @mfarim
    @mfarim 6 ปีที่แล้ว +1

    Thanks. Good Explanation...

  • @BiscuitTech
    @BiscuitTech 6 ปีที่แล้ว

    This was really useful, thanks!

  • @berkekaancetinkaya8721
    @berkekaancetinkaya8721 4 ปีที่แล้ว +2

    Shouldn't you have used the prevState parameter while incrementing the state.count value? this way you are editing the state directly its not the best practice, also in Docs using prevState parameter is the best practice

  • @natashakanji3313
    @natashakanji3313 4 ปีที่แล้ว

    you made my day thanks

  • @jamshyek
    @jamshyek 3 ปีที่แล้ว

    Nice explanation

  • @oskarolortegui9623
    @oskarolortegui9623 4 ปีที่แล้ว

    So cool, please help me with this. why do you use the methods without using the ".bind(this)" into the constructor? what's the difference between doing in that way from the other way?????

    • @artembelyshev9467
      @artembelyshev9467 4 ปีที่แล้ว +2

      You can use both methods depends of your situation. Using of arrow functions with ES6 is equivalent of binding to constructor

  • @rajbannasa7662
    @rajbannasa7662 3 ปีที่แล้ว

    Thank you so much sir ❤️

  • @mohammedhasanshaikh4800
    @mohammedhasanshaikh4800 4 ปีที่แล้ว

    Really love your content..!!

  • @VictorGarcia-si8wy
    @VictorGarcia-si8wy 6 ปีที่แล้ว

    You sound just like Beau Carnes lol. Thanks for the content!

  • @azelsky
    @azelsky 6 ปีที่แล้ว

    if we need to load up some data from a server, for example, we loaded articles and it contains some comments but data which we have contains the only id of comments. Based on it we need to make a request to receive comments.
    Do we make this request in ComponentDidUpdate?
    Because before I did it in componentWillReceiveProps but now it is legacy

    • @wilsongaturu134
      @wilsongaturu134 5 ปีที่แล้ว

      Hi, did you solve this? I have the exact same situation. The comments need to be fetched separately and I am not sure which life cycle method to use.

    • @nickkarnik9688
      @nickkarnik9688 5 ปีที่แล้ว

      Yes, you should make that request in ComponentDidLoad (If I understand what you're asking correctly).

    • @nickkarnik9688
      @nickkarnik9688 5 ปีที่แล้ว

      Looking at the above comment from Wilson, I think I understand what you're asking. If you want to load comments later on, you will need to do it after your primary data loads (either in the callback or when the promise resolves). Otherwise, you might need some trigger such as clicking a button to show comments, which will make the call.

    • @wilsongaturu134
      @wilsongaturu134 5 ปีที่แล้ว

      +Nick Karnick What we ended up doing is creating the action that fetches comments (we were using Redux) and then chaining it to the fetch articles action using ".then". This was done in the ComponentWillMount method. Since we also had comment replies that required the parent comment id, we had a fetch replies button to trigger that event as you suggest.

  • @bharathsam4355
    @bharathsam4355 2 ปีที่แล้ว

    Good explaination

  • @keevnx
    @keevnx 5 ปีที่แล้ว

    What is the difference when I put the state inside constructor and state outside constructor, what is the difference?

    • @nickkarnik9688
      @nickkarnik9688 4 ปีที่แล้ว

      Kevin Adam it’s the same. Within the constructor you have access to props.

  • @shaifuddin
    @shaifuddin ปีที่แล้ว

    Please do a Mantine hooks project

  • @CarlWicker
    @CarlWicker 5 ปีที่แล้ว

    Great video

  • @ast453000
    @ast453000 3 ปีที่แล้ว +6

    too much focus on secondary LM methods, and not enough on the primary ones

  • @dhrunit1018
    @dhrunit1018 4 ปีที่แล้ว

    Thank you for this :)

  • @kavitasisodiya5846
    @kavitasisodiya5846 3 ปีที่แล้ว

    Can anyone answer that why did he used "disable={this.state.mount}"

    • @ast453000
      @ast453000 3 ปีที่แล้ว

      this.state.mount === true in the initial state. Look at the state in the constructor. So in the initial state disabled = {true} for that button. When mount is updated in state, disabled will also be updated.

  • @Jason06245
    @Jason06245 ปีที่แล้ว

    does anyone know why does my browser console print twice everything? like it prints "constructor" and "render" twice respectively follow this tutorial.

    • @davidlin7968
      @davidlin7968 ปีที่แล้ว

      might have to do with using strict mode

    • @someone-on-earth
      @someone-on-earth 7 หลายเดือนก่อน

      you must've figured it out by now, but I'll post this for anyone facing this.
      this behavior of react happens only in development and its there to help us catch bugs ahead of time and its actually a feature and not a weird behavior, you should not opt out of this, but even then if you do then you can remove the wrapper.

  • @karangupta725
    @karangupta725 4 ปีที่แล้ว

    dont know why in my code ,render gets printed twice in console?

    • @sagemode37
      @sagemode37 4 ปีที่แล้ว

      Maybe you have out console.log('render') in render() method of App.js as well counter.js

  • @harshavamsikalluri
    @harshavamsikalluri 6 ปีที่แล้ว +1

    Thanks a lot!

  • @bilelrahmouni01
    @bilelrahmouni01 4 ปีที่แล้ว +1

    are you explaining or showing off how rapid you are on the keyboard?

  • @martinharrisFTW
    @martinharrisFTW 5 ปีที่แล้ว

    good stuff! thank you

  • @prathameshausekar
    @prathameshausekar 2 ปีที่แล้ว

    Thanks a ton!

  • @kwaku_2023
    @kwaku_2023 2 ปีที่แล้ว

    @freecodecamp
    how do you set( this.setState({error, info) ) the error and info to state when it's not initialized in state (this.state), my conditional block is also not running after encountering error in nested component:
    if (this.state.error) {
    return Error Found!;
    }

  • @waqaspathan3337
    @waqaspathan3337 ปีที่แล้ว +1

    great video but take "hooks" out of the title, its misleading

  • @crocodileman7790
    @crocodileman7790 6 ปีที่แล้ว

    my generate see not working, how is that ?

  • @JenniferStewart100
    @JenniferStewart100 4 ปีที่แล้ว +1

    Now that react added hooks, this is obsolete.

    • @H3000-v7i
      @H3000-v7i 3 ปีที่แล้ว +2

      Typical newbie comment

    • @spencerknight1730
      @spencerknight1730 3 ปีที่แล้ว

      @@H3000-v7i lmao thats all programming videos. newbies trying to be the one with the information

  • @kingwindie
    @kingwindie 6 ปีที่แล้ว

    ummmm, what type of vs code is this?

    • @nickkarnik9688
      @nickkarnik9688 6 ปีที่แล้ว

      cruz I’m using Webstorm in this video. However, I would also recommend checking out CodeSandbox as I’ve used in my recent videos on my channel.

  • @abdulghani786
    @abdulghani786 4 ปีที่แล้ว +5

    Where the heck is hooks?? Misleading title 😐

    • @echonabin
      @echonabin 4 ปีที่แล้ว +1

      He is not referring to functional hooks instead it's component life cycle hooks that's what it is said so don't be confuse i guess in functional hooks and this life cycle hooks!!

  • @johndifelice7289
    @johndifelice7289 ปีที่แล้ว

    Where are the hooks?

  • @ayushgupta6214
    @ayushgupta6214 4 ปีที่แล้ว

    can i get the code??

  • @zerosku96
    @zerosku96 4 ปีที่แล้ว

    Here I am browsing through React content to find out why my **** doesn't re-render. XD

  • @sivaganesh4489
    @sivaganesh4489 4 ปีที่แล้ว

    Awesome

  • @vikashnirwal
    @vikashnirwal 4 ปีที่แล้ว

    Thanks

  • @aashikothari3916
    @aashikothari3916 8 หลายเดือนก่อน

    😊

  • @letsmakethat2364
    @letsmakethat2364 5 ปีที่แล้ว

    I watched the entire video and learned very little about React Hooks

    • @HaxGuru
      @HaxGuru 5 ปีที่แล้ว +3

      Lifecycle Hooks has nothing to do with React Hooks.

  • @shashanksharmadon4295
    @shashanksharmadon4295 4 ปีที่แล้ว

    Nyco.....

  • @nehakhandelwal6237
    @nehakhandelwal6237 3 ปีที่แล้ว +1

    Cannot understand a single thing why what is happening

  • @sarcasticdna
    @sarcasticdna 4 ปีที่แล้ว

    ❤️❤️❤️

  • @ahmedmahfouz6339
    @ahmedmahfouz6339 6 ปีที่แล้ว

    Please try vue it's more powerful

    • @GM-xk1nw
      @GM-xk1nw 6 ปีที่แล้ว +1

      no, it's just differente

    • @ahmedmahfouz6339
      @ahmedmahfouz6339 6 ปีที่แล้ว

      @@GM-xk1nw mention the difference

    • @Deliverant
      @Deliverant 6 ปีที่แล้ว +2

      It's not more powerfull, it's "just" vastly easier to learn and super fun to use

    • @Albertmars32
      @Albertmars32 6 ปีที่แล้ว +2

      The fact that react is industry standard tells me the opposite

  • @kinguinkng4403
    @kinguinkng4403 3 ปีที่แล้ว

    U have micro for ASMR. Please stop using it for guides.

  • @dec23
    @dec23 5 ปีที่แล้ว

    Not helpful

  • @vishaljadhav-lf3km
    @vishaljadhav-lf3km 6 ปีที่แล้ว

    First comment :)