This New React Hook Breaks All The Rules And I Love It

Поділитися
Вставка
  • Опубліковано 26 сер 2024

КОМЕНТАРІ • 264

  • @YogevTube
    @YogevTube Рік тому +620

    I love how they ran out of names for hooks so they just went with "use"

  • @HorstKirkPageKian
    @HorstKirkPageKian Рік тому +220

    Not only do hooks kind of break the rules of JavaScript, now there's a hook that breaks the rules of hooks. That's great!

    • @xgtwb6473
      @xgtwb6473 Рік тому +4

      Hi sir how do they break the rules of JavaScript? I am not disagreeing I want to learn. Thanks

    • @cafebean
      @cafebean Рік тому +2

      @@xgtwb6473 they don't actually break the language, they provide a useful abstraction. his comment was in jest

    • @jrcenina85
      @jrcenina85 Рік тому +3

      @@cafebean
      yes: in jest -
      to test
      who’s asleep
      (or not…)

  • @tuqe
    @tuqe Рік тому +16

    I didnt realize how much I was feeling the rules of React, seeing a hook inside a conditional statement makes me feel dirty

  • @dave6012
    @dave6012 Рік тому +19

    React keeps talking about how they’re going to solve this problem. All I say is, “promises, promises…”

  • @bideshbanerjee5506
    @bideshbanerjee5506 Рік тому +5

    that can be achieved without even using "use" hook, any component wrapped in a suspense can throw a promise and cached that result in a map. We can make it work like if the data is not present in the map throw promise(fetch data) or if it's present in map(cached) use that

    • @troy5370
      @troy5370 Рік тому +2

      Yeah lets make it rocket science

    • @MichaelKire
      @MichaelKire Рік тому +1

      You're right. This is basically a promise to suspense (thrown promise) converter in some sense.
      But still something that should, in my opinion, have been a part of the Suspense release from the get go. And should probably also have been named to something a bit more descriptive that what it is now.

  • @Euquila
    @Euquila Рік тому +86

    It'll still be another year (or two) before we start using this in a production environment. The cost of getting the team to adopt something like this has to be considered. Think holistically about your projects and remain balanced. Don't jump on every new shiny thing that reveals itself

    • @FrameMuse
      @FrameMuse Рік тому +5

      This also may be rejected and we wouldn't see it at all

    • @huge_letters
      @huge_letters Рік тому +6

      Doesn't tanstack-query already exist for this kinds of problems?

    • @z-aru
      @z-aru Рік тому

      @@huge_letters using tanstack-query means that your website needs to load JS first, this would hurt SEO for web crawlers other than Google (Google web crawler can tolerate a js load on first visit). tanstack-query is a js library after all.
      While on the other hand, server components gives server rendered html, so that web crawlers could see it as html first, rather than to have to wait for js to load and initialize.

    • @huge_letters
      @huge_letters Рік тому

      @@z-aru and this hook provides some SSR functionality which is not available with tanstack?

    • @z-aru
      @z-aru Рік тому

      @@huge_letters Nvm, I was wrong about this new hook, this hook does run on client side and is the first one that could be used conditionally.

  • @me_rinta
    @me_rinta Рік тому +78

    You can argue that it’s not that much useful or simpler. If you want to handle errors or loading state in multiple components individually you still need to wrap the components with Suspense and ErrorBoundary

    • @4w0ken
      @4w0ken Рік тому +10

      exactly what i was thinking.. only the tools change

    • @MatanPresberg
      @MatanPresberg Рік тому +2

      I think part of the point is you can handle loading and error states for as many nested components as you want and each can have their own async data fetches. No managing loading states across components and such. Also dealing with loading and error outside the components can make for more readable components.
      Could be off on the details I've never used this, but we do use Error boundaries at work.
      Ultimately though, you're right, you still have to tell it what to do on error and load, and this is just a different way to write it.

    • @phykk
      @phykk Рік тому +5

      Will there ever be a official functional way of using ErrorBoundary instead of using a class?

    • @kraldada6557
      @kraldada6557 Рік тому

      @@MatanPresberg But if you wrap the error around everything then a lot of time you gotta do different things on error... So you have to create another async function to wrap around your call to give it some id or something to be recognizable... Its getting redux wibes again. The simpler the code the better. This is not cool.. I dont see why the use cannot just give you a onError & onLoading... this is half-made again.

    • @MatanPresberg
      @MatanPresberg Рік тому +1

      @@kraldada6557 you get to choose what you wrap it around, React will just hit the closest Error boundary in the tree. If you wanted to set variants from components you could always use Context and have props passed in from Context to loading or error boundary.
      But if you don't like the pattern you don't have to use it. I'm just explaining it's use as best I can / as best as I understand

  • @aleksd286
    @aleksd286 Рік тому +1

    Bro, no way thats easier 😂😂😂. React-query solves all of this in one line

  • @slaffsla
    @slaffsla Рік тому +1

    Nice, you just sold me your course! Oh, it is free, lol :) Great work and I truly appreciate your content and the fact your course is free.

  • @subeg
    @subeg Рік тому +3

    Can you do preact signals for react

  • @mertdr
    @mertdr Рік тому +19

    Great video again Kyle. Only thing you haven’t mentioned is how does it get affected by component’s re-rendering (maybe I missed that part). I’m guessing it doesn’t re-render its function. If it’s so the question would be how to re-render it? (Refetch in this case)

    • @austinstewart1451
      @austinstewart1451 Рік тому +3

      Agreed. I also wonder about retry logic. I'd guess we have to handle it within the promise before finally deciding to resolve but then it would be difficult to give updates to the user during the process since Suspense retains it in the meantime. Could probably have some sort of handler in the parent but then that would require manually rerendering the component as well.

    • @mertdr
      @mertdr Рік тому +4

      @@austinstewart1451 yes that’s a good point. So far the only benefit I can think of is running a promise without wrapping it inside an async function. It could be useful in useEffect.

  • @WebDevelopmentWithSS
    @WebDevelopmentWithSS 13 днів тому +1

    we can use also swr or react query for this.

  • @codinginflow
    @codinginflow Рік тому +2

    use looks kinda like the syntax of SWR or React-query to me

  • @zlackbiro
    @zlackbiro Рік тому +47

    As I mentioned earlier, I'm an experienced React and Node developer. Therefore, I advise against using or exploring any experimental features in React. The 'use' hook has been marked as experimental for the past couple of months, and we're unsure whether it will ever become a production-ready hook. Therefore, it's best to focus on using only reliable and tested features. It's essential to be wary of content created by UA-camrs who may produce videos simply to have something to publish. Although Kyle is a good guy, it's important to understand that every UA-camr needs to produce content regularly.

    • @nigelminor5624
      @nigelminor5624 Рік тому

      Thank you, please don't stop commenting to all videos giving newbies like me help we need.

    • @sekgo1265
      @sekgo1265 Рік тому

      "As I MeNtIoNeD eArLiEr"
      ... and then I stopped reading because you already sound full of yourself lol

  • @AlexBlack-xz8hp
    @AlexBlack-xz8hp Рік тому

    Great video. Thanks for taking the time to make this. Very helpful.

  • @tonnoztech
    @tonnoztech 4 місяці тому

    would love a comparison video on how this compares to tanstack query!

  • @RemX405
    @RemX405 Рік тому +5

    Or.... you just extract your default fetch routine/Error handling/Loading component stuff inside a function/custom hook, you code it once, and you reuse it everywhere, and your error handling state is still local to your component. The error boundary thing seems like a nice fallback, but not a proper way to handle errors for specific requests. All these indirections kinda goes against the principles of a declarative react component IMO.

    • @JC-yy5nf
      @JC-yy5nf Рік тому

      And these new fetch functionalities along with errors and loading states already have had their logic updated with the newest React Router. I don't see the point of this hook, maybe I just don't understand this new hook enough yet.

    • @JamesScholesUK
      @JamesScholesUK Рік тому

      Yep. Mine's called useDataFromAPI, what's yours?

  • @RamiGB
    @RamiGB Рік тому +1

    Embrace yourselves for components full of nested and conditional use hooks. Good luck PR reviewers :D .. jokes aside this looks amazing, thanks for the video man.

  • @mulwelimushiana8388
    @mulwelimushiana8388 Місяць тому +1

    It seems like an easy hook to use. Pun definitely intended 😜

  • @mmocny
    @mmocny Рік тому +1

    Doesn't use+fetch.then cause infinite network requests? It's like the useEffect without deps footgun. You have to ensure the promise it stable across renders, and can use cache() for that, before handing it use()

    • @usmanabdurehman97
      @usmanabdurehman97 11 місяців тому

      yeah. I was trying that and it caused an infinite number of requests

  • @ritudey8520
    @ritudey8520 Рік тому +1

    I love ur videos, thanks a lot for ur simplification and keeping it short.

  • @darkmift
    @darkmift Рік тому +1

    I feel like in this use case the boilerplate changed and all it does to useit correctly is to move it around.
    Might aswell use react query.

  • @very_unique_username
    @very_unique_username Рік тому +7

    Cool video, but this example is a bit broken. Every render of Data component will trigger a refetch, since `use` takes in a simple expression which doesn't have a stable function identity. This can be fixed by wrapping `fetch` in a `useCallback` hook or by creating a function that returns a Promise outside the component scope.

    • @eukante
      @eukante Рік тому

      What is this new gen z name for function reference :D, also I'm not sure about the usage of the word stable - implies that a function reference might be unstable? :))))
      Also using useCallback is not the correct approach, defining the api call as a constant or a function will achieve the same effect, but will use less memory.
      You're welcome for the free PR review :DDD

    • @very_unique_username
      @very_unique_username Рік тому

      @@eukante idk, in JS circles people mention stable function identity and stable function reference almost interchangeably, just went with whatever came first from the top of my head. “stable” has always meant that the reference is non-volatile or static or whatever you feel like calling it, basically that it just doesn’t change on its own, not sure why you are nitpicking this.
      I do agree that using ‘useCallback’ is not the prettiest approach, hence why I’m also suggesting to create a fetching function outside the component scope, just as you like it 👍 Memory concerns are a separate topic that I don’t really wanna talk about, but I don’t see this as a problem unless you’re running your JS code on an outdated engine inside of an embedded system lol. Engines are smart and preemptive micro optimisations are bad for your sanity.

  • @bibahbibah5108
    @bibahbibah5108 Рік тому

    very useful hooks it simplify the service call, and tkx man

  • @theisoj
    @theisoj Рік тому +2

    Thanks Kyle!

  • @PeIeus
    @PeIeus Рік тому

    I wish I was as good as you, great content you got a new sub

  • @damonwu9658
    @damonwu9658 9 місяців тому

    React query is better choice, but this is a cool tool as well 👍

  • @joelbrighton2819
    @joelbrighton2819 Рік тому +4

    Maybe I'm missing something here but with the original technique the 'loading' and 'error' handling were all contained in the original component. Now, with Suspense and ErrorBoundary we have pulled this logic out of the original component into the host component. So, every time we use the revised component we have to repeat this logic or, more likely, wrap it inside another component which contains just the ErrorBoundary, Suspense and revised component. Is this really an improvement? Am I just not seeing the light?

    • @ancellery6430
      @ancellery6430 Рік тому +1

      i agree, i dont really see anything different with the previous code and new code. in total it is 2x as much code because of the new components rather than just keeping the error and loading in the componenet.

    • @zerofirex12
      @zerofirex12 Рік тому +2

      I was just about to respond with the same question. It seems more like code was just re-organized and not so much improved per-se.

    • @vvan22
      @vvan22 Рік тому +1

      Then we should wrap it with another wrapper component. Lol, yeah I agree with you, it looks like making the component cleaner, but actually it creates another problem or just another technique to solve nothing. But at least it's good for a content creator to make another video about React. Lol.

    • @martijn3151
      @martijn3151 Рік тому

      And this is probably why it’s experimental

  • @asdasd-jg1re
    @asdasd-jg1re Рік тому

    quick and good for small projects, but still have to use react query or swr because of cache i guess

  • @wizardoflightnings6841
    @wizardoflightnings6841 Рік тому +1

    Thanks man, it's awesome!

  • @anvarsaidov8964
    @anvarsaidov8964 Рік тому

    Thank you for suspense and errorBoundary.

  • @haniamania5884
    @haniamania5884 Рік тому +28

    Super są te kursy!

  • @tiriana2
    @tiriana2 Рік тому

    I'll stick with react-query, but will keep an eye on this little hooky

  • @user-es2vr3cv1v
    @user-es2vr3cv1v Рік тому

    Amazing 🤩 it’s so easy to use and understand

  • @eleah2665
    @eleah2665 Рік тому

    Very good. Will wait for it to be officially released. Thanks.

  • @25kirtan
    @25kirtan Рік тому

    Amazing video Kyle

  • @mohanram18
    @mohanram18 Рік тому

    I have created the exact use case for custom hook for fetching, they could have created a concept of utils rather than hooks.

  • @atishchandole
    @atishchandole 5 місяців тому

    Could you make video that will auto translation in different langauages

  • @alexanderkomanov4151
    @alexanderkomanov4151 Рік тому

    So useful! Thanks!

  • @kanways8
    @kanways8 Рік тому

    Thanks for video. Can you please make video about how to make react reusable form validator with minimal code

  • @aneeqak
    @aneeqak Рік тому

    Bro please make videos on How to structure React App and how to write clean code.

  • @shantanusharma4901
    @shantanusharma4901 Рік тому

    Support you sir 🎉

  • @axelj.solares5076
    @axelj.solares5076 Рік тому

    This was mind blowing!!

  • @seobookpro
    @seobookpro Рік тому

    What about pass cors when fetch external data
    Promise/await not help if the external server don’t have cors enabled
    This is for internal works

  • @peopledrivemecrazy
    @peopledrivemecrazy Рік тому +3

    This took them years but this is the first thing that has impressed me in years from react.

  • @palyanytsia
    @palyanytsia Рік тому

    u are the best with explaining new features ❤❤

  • @koderkev42
    @koderkev42 Рік тому +2

    "This New React Hook Breaks All The Rules", sums up why many devs dread front-end development. JS allows (and often encourages) so much hacky code that it becomes a nightmare to maintain. Solutions to bugs/problems often involves hunting down yet another library or version update. I'm for constant-learning but this is madness.

    • @gwapster13
      @gwapster13 Рік тому +2

      They wanna make JS devs look super smart.

  • @kohelet910
    @kohelet910 Рік тому

    Can't wait for this new hook to be released :)

  • @samueldonsmog6006
    @samueldonsmog6006 Рік тому

    use Hook seems like a compression of all the previous Hooks. impressive

  • @cinques
    @cinques Рік тому

    fetch promise reference isn’t stable so it will be infinite rerenders

  • @densfloar
    @densfloar Рік тому

    Did you do already a video on useSignal? Is it also in experimental?

  • @NathanBudd
    @NathanBudd Рік тому

    I didn't quite follow the Context bit at the end... does this mean you could use the "use" hook in the context to fetch data, then share it across multiple components?

  • @ronaldomaia
    @ronaldomaia Рік тому

    Nice video. I wonder what's going to be if we use this hook with React Query

  • @pimpakastewart27
    @pimpakastewart27 Рік тому

    loved !!!!

  • @veritas7010
    @veritas7010 Рік тому

    React in itself is a boilerplate for corporate abstraction

  • @eugenepranoto3824
    @eugenepranoto3824 Рік тому

    May i ask how the getderivedstatefromerror trigger if we are using react redux query?

  • @hamidmohamadi2943
    @hamidmohamadi2943 Рік тому

    Great kyle, thanks, Q: what about useEffect dependencies?🤔

  • @HiImKyle
    @HiImKyle Рік тому +2

    Much better, even in experimental it's already a 1000x better

  • @skateboarderlucc
    @skateboarderlucc Рік тому

    How is placing the error handling markup outside of the component it refers to better?

  • @rishiraj2548
    @rishiraj2548 Рік тому

    Thanks

  • @LeFede
    @LeFede Місяць тому

    It works but it gives me the error:
    "A component was suspended by an uncached promise. Creating promises inside a Client Component or hook is not yet supported, except via a Suspense-compatible library or framework."
    Any solution?

    • @CODERSNEVERQUIT
      @CODERSNEVERQUIT Місяць тому

      what example he is showing is wrong data fetching is done on server component and you have to pass the promise to client component to consume it using use hook

  • @troy5370
    @troy5370 Рік тому +7

    Doesn't it break the notion of component being self contained? You have to know about the component in order to use suspense and it effects rendering outside of the component being used. Thus this component is not longer isolated or testable as your going to handle loading and errors outside the component.

    • @Zechey
      @Zechey Рік тому +3

      true I see this making the code as a whole just more complex, I personally dont mind having those extra lines of code in a separate component

    • @Xetoxyc
      @Xetoxyc Рік тому +1

      because of that its especially usefull on ssr which is why next.js 13 want this feature so hard

  • @Nil-js4bf
    @Nil-js4bf Рік тому

    I have no idea how it works behind the scenes but being able to run hooks conditionally? Hell yeah. I hated having to create container components that conditionally rendered just to be able to conditionally run a hook. Or doing things like passing `skip: true` to the hook that fetches gql data.

  • @mindaugasazubalis
    @mindaugasazubalis 11 місяців тому

    Awesomeness 🎉🎉🎉

  • @mohithguptakorangi1766
    @mohithguptakorangi1766 Рік тому +2

    If they could allow a hook in an if statement, why did they restrict it in the first place (for other hooks - useState, useEffect,...) ? There must be some disadvantage that comes with it.

    • @kerimgueney
      @kerimgueney Рік тому +1

      Yeah, I suspect it causes all sorts of component lifecycle issues.

  • @クバ
    @クバ Рік тому +1

    They should name it useUse

  • @gauravpoudel7288
    @gauravpoudel7288 Рік тому

    Ur awesome man

  • @_justnick
    @_justnick Рік тому

    ... So, they're basically moving the boilerplate code up to the parents and making everything more "magical" (aka confusing)

  • @MaxArt2501
    @MaxArt2501 Рік тому

    My question is: HOW?
    The reason why hooks can't be used inside `if` statements is that they're tracked in sequence. A branch condition (or an iteration) breaks the sequence.
    This is important because hooks aren't named or tied to the component instance in any way, so React needs to keep a stack in order to perform the correct action.
    But if you can put `use` inside an conditional it means that's not actually tracked. So what's the secret?

  • @chadvansyoc11
    @chadvansyoc11 Рік тому

    Hey I imported experimental version of react but the "use" hook wasn't there. Anyone know what I might have done wrong?

  • @alsteant
    @alsteant Рік тому

    Very nice!

  • @HzHzHzHz111
    @HzHzHzHz111 Рік тому

    the useEffect had the url in the dependency array so it won't keep fetching the data on every render, does use do the same?

  • @lisofsky8151
    @lisofsky8151 Рік тому

    спасибо !

  • @jason_v12345
    @jason_v12345 Рік тому +2

    Seems like something any one of us could implement for ourselves in about 10 minutes.

  • @m.lutfibadila9225
    @m.lutfibadila9225 Рік тому

    I'm lovin' it

  • @HoomanFard
    @HoomanFard Рік тому

    Hi @Kyle and Everyone, Do we know when use() hook will be release in stable version of react?

  • @JohnnySalami-jo4jh
    @JohnnySalami-jo4jh Рік тому

    I don't really see the value of "use" when it just replaces async/await??

  • @arjundureja
    @arjundureja Рік тому

    Is there a benefit to using this over react query?

  • @tornikebuchukuri4114
    @tornikebuchukuri4114 Рік тому

    is it possible to access error in Data component though? if not it's really bad

  • @BryantWooters
    @BryantWooters Рік тому

    Is this hook able to be used within a JavaScript class?

  • @rcknr
    @rcknr Рік тому +4

    This is classic React: introducing a brand new thing which is unlike anything you know but so much better!

  • @ytadi9229
    @ytadi9229 Рік тому

    Weird how we still end up class based component just for ErrorBoundary

  • @vivekkaushik9508
    @vivekkaushik9508 Рік тому +2

    This is so cool. Very useful. Can't wait for this to be GA.

  • @SXsoft99
    @SXsoft99 Рік тому +1

    Will be honest, i have been developing on the web for the past 6years, mainly in php woth jquery, react, and vue for the front, now i am using inertiajs and to be honest i dont understand all the hype with asynchronous requests, loading microcomponents in your main component, i have never found the use for it, plus most apps online kinda have one main big load then micro lazy loads on demand
    Besides showing us exemples of code can you also exemplify some real life examples of applications?

  • @Richard_Nixon-mr6rq
    @Richard_Nixon-mr6rq Рік тому +1

    Why was the Error Boundery a class component?
    I thought you weren't supposed to use class components anymore?

    • @FrameMuse
      @FrameMuse Рік тому +1

      Class components are not deprected and this is only way to create a error boundary so far.

    • @justarandomname
      @justarandomname Рік тому

      you can only implement Error Boundary using class component, there are libs that abstract the implementation into a functional component but, it basically just a class component

    • @Richard_Nixon-mr6rq
      @Richard_Nixon-mr6rq Рік тому

      @@FrameMuse huh, I thought always thought they were deprecated that’s good to know

    • @Richard_Nixon-mr6rq
      @Richard_Nixon-mr6rq Рік тому

      @@justarandomname good to know 👍

  • @solevow
    @solevow Місяць тому

    So How Can I retry a new promise after my use hook promise rejected?

  • @lemon_maho
    @lemon_maho Рік тому

    I have a question about fetching data, I'm using react query in all my fetch requests, is that ok?

  • @driverjb09
    @driverjb09 Рік тому

    I'm not very familiar with React. About how long is it before things go from experimental to "production ready"?

  • @charliesta.abc123
    @charliesta.abc123 Рік тому

    Sounds like a big WIN for react then!

  • @GreggBolinger
    @GreggBolinger Рік тому

    Neat, but it's just parts of react-query's useQuery. And if use() doesn't at least do what useQuery already does, why would I use use()? use use use use use.

  • @BayuWicaksono
    @BayuWicaksono Рік тому

    Does this killing useQuery?

  • @gonzalooviedo5435
    @gonzalooviedo5435 Рік тому +2

    Why are free, your content is awesome!

  • @linorabolini
    @linorabolini Рік тому

    I hope they adopt solidJS approach. It’s way simpler

  • @gonzalooviedo5435
    @gonzalooviedo5435 Рік тому +1

    Este es un primo de kale Anders?

  • @enkhbayarnyambayar9400
    @enkhbayarnyambayar9400 Рік тому

    Oh okay react finally become more developer friendly

  • @kantyDarius
    @kantyDarius Рік тому +1

    this is pure gold, i´ll start use it in my daily job inmediatly, thanks a lot Kyle

    • @eukante
      @eukante Рік тому

      Please don't.
      But I guess this was satire and I got screenshotted into r/woooosh

  • @moishinetzer
    @moishinetzer Рік тому

    Will this also be a hook for react native?

  • @henhhenh2148
    @henhhenh2148 Рік тому

    i love it.

  • @Eshironde
    @Eshironde Рік тому

    It looks nice, but it feels like React is just reinventing JavaScript that you have to write with React syntax :P

  • @srsly4242
    @srsly4242 Рік тому

    so the use hook is basically react-query with less features.