How to share a React Component with the URL

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

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

  • @Shaker-Hamdi
    @Shaker-Hamdi 2 หลายเดือนก่อน +28

    Hands down, the best React instructor out there.
    I would love if you have a full course on edge cases that I could buy.

    • @samselikoff
      @samselikoff  2 หลายเดือนก่อน

      Love this idea! Any you have in mind off the top of your head?

  • @deiminator2
    @deiminator2 2 หลายเดือนก่อน +11

    Bro you hit the nail in the head with this one, the problem is, most of the time a search is not only a search, more often you also have extra filters for the query, and pagination, other times you also need to consider the load in the server so you have to add debounce, which a lot of devs won't be able to implement without introducing state.
    So that might be a good video idea, a full search tutorial, keep up the good work!

  • @bizmich_
    @bizmich_ 2 หลายเดือนก่อน +13

    It is very important to add a debouncer as well. Otherwise, there will be too many requests to the backend.

  • @mdrifat6941
    @mdrifat6941 22 ชั่วโมงที่ผ่านมา

    Long ago you made a video title something like "Lifting State Up" where you might have shown the same logic. But at that time I was new to the React ecosystem and did not want to feel overwhelming. But today I felt the need of this technic and was searching for the video but could not find that as "Lifting State Up" is an established jargon in the react world for almost the same topic.
    So, as I already have subscribed you, so I searched for your channel in my subscription list and wala, found that you covered the same topic in your last video also. Thanks a lot.

  • @osamammursleen
    @osamammursleen 2 หลายเดือนก่อน +7

    Please add regular videos for these kind of things. Its fun learning from you.

  • @Pharoxx105
    @Pharoxx105 2 หลายเดือนก่อน +2

    This info is actually useful for any project without relation to React or any other framework. Great job simplifying the problem and explaining it in a way that is easy to understand and solve.

  • @micahscheer2534
    @micahscheer2534 2 หลายเดือนก่อน +3

    I appreciate your authenticity and clarity Sam!

  • @CodeWithRomaen
    @CodeWithRomaen 2 หลายเดือนก่อน +4

    You are such an awesome educator. It's unreal!

  • @Ahmad_5000
    @Ahmad_5000 2 หลายเดือนก่อน +7

    Beautifully explained as always, thanks Sam!

  • @RonnieDenzel
    @RonnieDenzel 2 หลายเดือนก่อน +1

    Thanks for saving the day again man,this was a lifechanger as i had a bug like this😅

  • @havefun5519
    @havefun5519 2 หลายเดือนก่อน

    two-way flow and the source of truth explanation is very clear.

  • @soumyanilpodder8435
    @soumyanilpodder8435 2 หลายเดือนก่อน

    Please make more such videos on React and concepts like this. Love the video.

  • @rockNbrain
    @rockNbrain 2 หลายเดือนก่อน

    nice job Sam, funny enough i did this yesterday at my job!! I'm happy that we're getting state back to the url

  • @PRonYouTube
    @PRonYouTube 2 หลายเดือนก่อน +10

    Thanks for another great tip, Sam! I wanted to ask about your thoughts on how this might spam the back button history with a lot of intermediary state values, i.e. every keystroke. I know that `router.replace` vs `router.push` might be one possible solution, but let's say we wanted to stick with router.push, but to just have it be less noisy. One compromise might be to avoid updating the URL after every keystroke, and do some sort of debouncing. But here's the catch: Is it possible to keep the UI updates instantaneous (so NO debouncing there), but while having the browser entry be debounced? With this current approach it doesn't seem possible at first glance, and might require separating out the source of truth into two sources again?

    • @samjohnston3416
      @samjohnston3416 2 หลายเดือนก่อน +3

      I would go with `router.replace` as for mobile this would be a nightmare. If I were to go to a page, type in some search then swipe back to the previous page, I would expect to go to the previous page, not the previously typed character. This use case would also break in the case of no js or slow network and you could rely of tag to use a normal

    • @joepetrillo6185
      @joepetrillo6185 2 หลายเดือนก่อน +3

      I was also going to comment this. I would use router.replace here. I don't think he would have found this bug with router.replace since the back button would just go the prior page and not consider each keystroke.
      @samjohnston3416 makes a good point about mobile swiping back and forth.

    • @samjohnston3416
      @samjohnston3416 2 หลายเดือนก่อน +1

      @PRonTH-cam as for your question regarding debouncing, you would just debounce the onChange function of the input that’s calls the router.push or replace

    • @jus.juraev
      @jus.juraev 2 หลายเดือนก่อน

      You should add debounce to your input onChange and instead of "value" you should use "defaultValue" of initial search value

    • @KirchikMart
      @KirchikMart 2 หลายเดือนก่อน

      I would also go with replace

  • @raoufnaoum7969
    @raoufnaoum7969 2 หลายเดือนก่อน

    Life saver😮
    Thank you so much

  • @ThugLifeModafocah
    @ThugLifeModafocah 2 หลายเดือนก่อน

    I love your videos, man. Thank you a lot.

  • @magnuserikkeenrobot1117
    @magnuserikkeenrobot1117 2 หลายเดือนก่อน

    yay your back

  • @rammehar5531
    @rammehar5531 2 หลายเดือนก่อน

    ❤ thank you very much, for all the wonderful videos

  • @ikbo
    @ikbo 2 หลายเดือนก่อน +1

    excellent video as usual. What course are you guys currently working on (I am a buildui premium member)?

  • @akg530
    @akg530 2 หลายเดือนก่อน

    This is such a helpful video!

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

    Great vid! But should add a follow up on how to add a debouncer. Having to press back for each letter is horrendous UX, no one would ship that! 😅

  • @zainuldin9095
    @zainuldin9095 2 หลายเดือนก่อน +1

    can we also push on un-focus to make back thing work correctly? and let suppose we have more params

  • @HeyNoah
    @HeyNoah 2 หลายเดือนก่อน

    Awesome!! This was great!

  • @HiteshJangid
    @HiteshJangid 2 หลายเดือนก่อน

    I also implement the same thing with react query and Mui datagrid along with search it also handle the pagination.

  • @BilalShahid-z7y
    @BilalShahid-z7y 2 หลายเดือนก่อน

    I always use this pattern when it comes to lifting state up to URL. But in case of search inputs, I usually use debounced change and "replace" instead of "push", because a layman user thinks he should go one page back when they press the back button.

  • @cedericprivat
    @cedericprivat 2 หลายเดือนก่อน

    What if we need to validate the searchParam? Let‘s say instead of a string for a input field, we have a select input, with a defined set of values. What if the user provides a value that is not in the set of the provided values. How would you handle that? Show an error or redirect to a default value? Or both?
    Would love to hear your insights on that!

    • @samselikoff
      @samselikoff  2 หลายเดือนก่อน +1

      Great question! Typically websites ignore them, so I would parse/validate them and if it's not what you expect just treat it as if it wasn't there. So the variable in your React app would be the default (empty string in the case of this video).

    • @cedericprivat
      @cedericprivat 2 หลายเดือนก่อน

      @@samselikoff Thanks for the quick answer!

  • @from.houseofgandhi
    @from.houseofgandhi 2 หลายเดือนก่อน

    Thanks. I will now start fixing my code.😆

  • @FranciscoGuerrero-i6g
    @FranciscoGuerrero-i6g 2 หลายเดือนก่อน

    Awesome video!

  • @drenns
    @drenns 2 หลายเดือนก่อน

    Nice work, Sam! This looks great. I have a similar use case where I added search/pagination functionality to a table, but now I need to include two or more tables on the same page. What’s the cleanest way to manage multiple tables? Should I use separate page parameters for each table (e.g., table1Page=2 and table2Page=1) to allow for independent pagination and search, or is there a better approach?

    • @sdglhm
      @sdglhm 2 หลายเดือนก่อน +1

      I'd probably use table[1][page]=1

  • @magnuserikkeenrobot1117
    @magnuserikkeenrobot1117 2 หลายเดือนก่อน

    could you explain why you use let instead of const for state variables etc?

  • @naolfekadu6101
    @naolfekadu6101 2 หลายเดือนก่อน

    can you please setup a stackblitz, replit or code sandbox so we could follow along?
    love your content

  • @FishTalkFish
    @FishTalkFish 2 หลายเดือนก่อน

    Well done!

  • @icestonks2555
    @icestonks2555 2 หลายเดือนก่อน

    You should check out nuqs, a really interesting state manager, that's trying to accomplish what you are making in this video.

  • @wingy3181
    @wingy3181 2 หลายเดือนก่อน +1

    could this get complicated to update the query string when there are multiple parameters? say there is two query params a and b...and two input fields for each query param respectively..........how could you easily only update the input field that was updated? My quick guess would be to use URLSearchParams?

  • @marcialabrahantes3369
    @marcialabrahantes3369 2 หลายเดือนก่อน +1

    now make this into a custom hook that gives you the logic to do this with *any* query param.
    usually it'd require a serialize and deserialize method, default value to remove the query param, and using the router as a patcher of k-v values rather than hard coded url

    • @franky47
      @franky47 2 หลายเดือนก่อน

      I''ve been working on this for over a year, you're on the right track. Add a throttled update queue to handle browsers rate-limiting URL updates, and you"ve got the `nuqs` package.

  • @aymenbachiri-yh2hd
    @aymenbachiri-yh2hd 2 หลายเดือนก่อน

    Thank you so much sam

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

    Perfect!

  • @Grozny446
    @Grozny446 2 หลายเดือนก่อน

    thanks, useful video

  • @rangestanrangestan
    @rangestanrangestan 2 หลายเดือนก่อน

    thanks for sharing but in every character input it sends a request ?

  • @harveyrice8504
    @harveyrice8504 2 หลายเดือนก่อน

    So how does the back button actually work? I had assumed it works the same as opening the previous irl in a new tab

  • @HazemAbdennadher
    @HazemAbdennadher 2 หลายเดือนก่อน

    on which version of next does this work on ?

    • @HazemAbdennadher
      @HazemAbdennadher 2 หลายเดือนก่อน

      cause using nextjs 14 this will cause a fullpage reload so it is lagging when updating the url

  • @jalalbmnf
    @jalalbmnf 2 หลายเดือนก่อน

    Ooookay, I neeed to fix something my code . Thank you for let me know, now I have a bug 😅

  • @adamjamiu6764
    @adamjamiu6764 2 หลายเดือนก่อน

    Yeah, it's either denounce is used or an abortController to cancel subsequent request and improve the user experience and performance

    • @samselikoff
      @samselikoff  2 หลายเดือนก่อน

      @@adamjamiu6764 React Query already ignores stale updates so an AbortController wouldn’t change the behavior here

  • @kashmirtechtv2948
    @kashmirtechtv2948 2 หลายเดือนก่อน

    ❤❤

  • @pokefreak2112
    @pokefreak2112 2 หลายเดือนก่อน

    I thought the bug was that hitting the back button removed a single character from the URL instead of actually going back to the previous page as it's supposed to...

    • @olalekanemmanuel6767
      @olalekanemmanuel6767 2 หลายเดือนก่อน

      Yea, I think that should be the behavior.
      Using router.push creates a new entry into the history stack , router.replace would be more suitable in this scenario

  • @steftrando
    @steftrando 2 หลายเดือนก่อน +1

    But I don't want the back button behaving that way