Stop Doing this as a React Developer

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 พ.ค. 2024
  • Javascript is a weird language and since it chooses a different path than other programming languages like C++ or Python it still has plenty of cool features we don't know about!
    So in this video, we'll explore some bad habits and mistakes we make as React developers and what's the best practice in order to avoid them, like doing conditional rendering wrong, not using debounce for API calls, or not knowing what forward refs is!
    ⭐ Timestamps ⭐
    00:00 Intro
    00:15 Conditional Rendering (&&)
    03:37 Debouncing
    09:56 Forward Refs
    ⚡️React Stop Doing This Repo
    github.com/ipenywis/react-sto...
    -- Special Links
    ✨ Join Figma for Free and start designing now!
    psxid.figma.com/69wr7zzb1mxm
    👉 ✨ Join Figma For Professionals And Start Designing with your Team ✨
    psxid.figma.com/ucwkx28d18fo-...
    🧭 Build Login/Register API Server w/ Authentication | JWT Express AUTH using Passport.JS and Sequelize
    • Build Login/Register A...
    🧭 Turn Design into React Code | From prototype to Full website in no time
    • Turn Design into React...
    🧭 Watch Tutorial on Designing the website on Figma
    • I Design a onecolor We...
    🧭 Watch Create a Modern React Login/Register Form with smooth Animations
    • Create a Modern React ...
    🧭 Debug React Apps Like a Pro | Master Debugging from Zero to Hero with Chrome DevTools
    • Debug React Apps Like ...
    🧭 Master React Like Pro w/ Redux, Typescript, and GraphQL | Beginner to Advanced in React
    • Master React Like Pro ...
    🧭 Learn Redux For Beginners | React Redux from Zero To Hero to build a real-world app
    • Debug React Apps Like ...
    🧭 Introduction to GraphQL with Apollo and React
    • Introduction to GraphQ...
    🐦 Follow me on Twitter: / ipenywis
    💻 Github Profile: github.com/ipenywis
    Made with 💗 by Coderone

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

  • @samwise8731
    @samwise8731 ปีที่แล้ว +359

    That's why some of us strictly use TypeScript.

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

      Yep

    • @johndoe-eu4ol
      @johndoe-eu4ol ปีที่แล้ว +7

      Doesn’t TS only check at compile time? If you fetch data from an API, TS cant know the data was a wrong type

    • @abhijithgowdar
      @abhijithgowdar ปีที่แล้ว +21

      @@johndoe-eu4ol There is GraphQL to share the type signature with the client. Going one step further there is tRPC for end to end type safety.

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

      I think the point he’s making is that this way of using conditional rendering can potentially render something that is not guaranteed to be a Boolean.
      Those solutions are definitely great for type safety when you own the full stack, but what if you don’t own the server you are making calls to?

    • @dinoscheidt
      @dinoscheidt ปีที่แล้ว +14

      Exactly. Use TypeScript. The bugs the creator describes are bugs due to not using a type system. Which is a big no no for logic.

  • @user-qg7lb1jx8b
    @user-qg7lb1jx8b ปีที่แล้ว +149

    The first one is incorrect… conditional rendering is not problematic in good code.

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

      Oh it can be if you use a numeric value, it'll attempt to illegally render the number 0 for instance. You can of course use a double bang.

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

      He could’ve used the Boolean(showWinning) function object instead, much cleaner and easier to understand.

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

      I discovered it as a problem with a third party controls children where it crashed on undefined controls. I’ve changed the hidden child controls to render as null to fix the code.

  • @jeromesnail
    @jeromesnail ปีที่แล้ว +339

    I'd argue that ternary is worse than conditional rendering. Maybe not in this the very example you're showing as it's one thing OR another, but in most cases if you just want render a single component conditionally then use... conditional rendering.
    It's not that hard not to assume the response of an API call and make sure to get a boolean anyway.

    • @daliovic24
      @daliovic24 ปีที่แล้ว +69

      I was a bit annoyed him calling it a MISTAKE, it's kinda clickbaity tbh

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

      @@daliovic24 I was gonna say the same thing and you two save e the time. thanks

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

      😂 agreed

    • @user-so2iw2mr5g
      @user-so2iw2mr5g ปีที่แล้ว +5

      using && is worse then ternary for rendering, for cases when you have an if else its for obvious reasons, but when you render a single component you suddenly switching syntax? if you can keep you syntax consistent, do it, its gonna be more readable that way.
      and I mean, with && your basically saying "I want to render the result of X AND Component", which works how you intend it too cause JS is weird but you realize how your statement is not what you actually wanna do right? meanwhile with ternary what your saying is "if X I want to render Component, else I want to render nothing" which is exactly what your trying to achieve
      I know I wouldn't pass a cr if I saw someone using && for rendering as in my eyes, its less readable and shows a misunderstanding of what the language is doing, and we shouldn't base our code on misunderstandings of the language if we can help it

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

      @@user-so2iw2mr5g you use another syntax because you're not doing the same damn thing.

  • @TheDorac1
    @TheDorac1 ปีที่แล้ว +206

    I disagree with the conditional rendering part - Using && is cleaner and easier. If the coder is using something other than a boolean, that's a bad coder that's not doing things the right way. The way we code shouldn't change because some devs don't know what a boolean is. 😛

    • @somebody-17546
      @somebody-17546 ปีที่แล้ว +5

      Right . I agree

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

      That's true, with ternary u can't use wrappers.

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

      @@RaZziaN1 elaborate please

    • @benkatz8999
      @benkatz8999 ปีที่แล้ว +15

      Also with typescript (which you should be using) it's a lot easier to ensure that your booleans are actually booleans.

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

      This is the correct answer, the only time something ‘unexpected’ happens is when the value ‘unexpectedly’ doesn’t act like a Boolean like the dev was hoping.

  • @silversurfer1707
    @silversurfer1707 ปีที่แล้ว +70

    1. Use TS + tRPC to ensure you have type safety.
    2. Use AbortSignal to invalidate previous requests when a new character is input. Much simpler.

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

      I haven't seen an abort controller in a long time. Think people just don't know that something like that is a thing.

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

      @@Voldrog yeah, I wonder why this isn't a more widespread knowledge.

    • @alexandrvienik1575
      @alexandrvienik1575 ปีที่แล้ว +10

      Abort signal is a good decision but it doesn't reduce amount of api requests

    • @deadlock107
      @deadlock107 ปีที่แล้ว +8

      2. It sends the same amount of requests, basically after every single keystroke. Doesn't solve the problem.

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

      Exactly. And for the first one I will add also to use triple equality. eg: showWinning === true

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

    I use lots of non-booleans in conditional rendering, but I always wrap them in `Boolean(data) && ( )`. It's the same as a double negation (!!data) but I find it more readable despite being a little longer. I dislike mixing lots of syntax symbols because a minor change in those symbols can have a big difference in the outcome, hence I prefer the very explicit Boolean() cast.
    However, lately I've taken inspiration from Solid and been using a ` ` component, since I find that to the most readable of all.

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

      umm yea that could be another way but that implies having tons of components

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

    I'm curious. Why not just double-negate your condition? Should be much easier to parse through your code. Ternary condition kinda looks awkward in JSX.
    Please correct me if I'm wrong.

  • @ayoub.k
    @ayoub.k ปีที่แล้ว +16

    For autocomplete, throttling makes way more sense than debouncing, and you can make the case for having a combination of both debouncing and throttling. When it comes to conditional rendering, you're just completely wrong, each way has its use case, and developers should know the pitfalls of something before using it.

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

      this

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

      This x2
      I did an autocomplete yesterday and I used throttling during writing, and debouncing to make sure that after user stopped typing the last search value is fetched

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

    The && for conditional rendering is in most cases the better solution, because a ternary operator returning null is unnecessary code

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

    I got the general concept of a debounce but found the code a little confusing - I'll figure it out i guess. And thanks for the forwardRef overview, I haven't had to use this yet but it will be handy to know when I eventually need it.

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

    Some great tips here! been a react dev for about a year now and I feel comfortable enough with the framework for sure, but you made me rethink again what I know best-practice-wise, and THANKS YOU for that!!

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

    Hey can you tell me which file icon you were using?

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

    One quick question: What is the vsc theme?

  • @aim-scom-lt9038
    @aim-scom-lt9038 ปีที่แล้ว +1

    Debounce good thinking this could really smooth things for my app project. PS: Really like the duality when sharing good code to better coding.

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

    I recently made another alternative to debouncing issue. It's technique to determine if user has copy and pasted value or actually typing value by calculating change of input text length. If user is typing user has explicitly need to click a button but if user has pasted value it will automatically trigger search function.

  • @scottserage9022
    @scottserage9022 ปีที่แล้ว +15

    Like others have said, the first example is not necessarily the best solution. The only issue is if the value being evaluated is a boolean. In your own example, you cast one of the values as a Boolean !showValue (or whatever it was). You could do the same thing above !!showValue

    • @user-so2iw2mr5g
      @user-so2iw2mr5g ปีที่แล้ว

      that's introducing unnecessary extra processing though, sure its not much but this things add up so why not just not add them in the first place if you can help it? casting tends to be an expensive action (though admittedly I'm not sure how expensive it is in JS, didn't check)

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

      @@user-so2iw2mr5g I'm not sure off the top of my head either, but I'd wager it is less than a tertiary which has to evaluate if the value is truthy or not any way

    • @user-so2iw2mr5g
      @user-so2iw2mr5g ปีที่แล้ว

      I mean, just like the tertiary the !!cast also has to evaluate if the value is truthy or not, hell they probebly use the same function behind the scenes to do this, its just thatbwith !! It has to call it twice while with tertiary it has to call it once, so theres that.
      In short, even though the savings in performance are admittedly, negliegble in most cases, if i had to put money on which is faster id go with tertiary
      The again there might be some kind of optimization baked into the language that somehow makes !! faster

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

    what is vscode theme?

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

    Imo its much better to avoid if statements overal in jsx and simply make an early return statement. If you end up with double jsx code, then it means that you havent properly split your components

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

    If you actually have an if+else condition in rendering, going for ternary operators (?:) is also my preferred way, but if your else is just "render nothing", so "condition ? something : null", going for "condition && something" is totally fine and also my preferred way.
    If your condition can be a falsy value that gets rendered by react (e.g. 0), just put a !! in front of it so parse it boolean. It's not a bad thing to do so.
    If you are scared of bugs changing your condition's type, this is not the right spot to fix it. You should generally always parse incoming data from a third party (an api, user input, etc.). You could use libs like Zod for that.
    But taking that as a reason to not use && or || operators is a weird way to think imo

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

    Another piece of information Thank you so much.

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

    03:37 Debouncing
    09:56 Forward Refs

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

    Second problem - why not write usual debounce function except this hook??

  • @erick-llerenas
    @erick-llerenas ปีที่แล้ว

    Thanks for the forward ref explanation

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

    Typescript identifies the boolean problem and makes surprises much less likely.

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

    7:14 , can we use onBlur instead of onChage , hence when focus is lost from input, the fetching happen , rather than each key press

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

      it would work, but you're describing a very non-standard user interaction and is guaranteed to confuse the user. If you're considering it on the blur event, might as well just put a button next to the input to perform the search. At least then the user will know they have to do something other than type to get the result.

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

      using onBlur makes it not autocomplete anymore, user should always see results for what he currently has typed in the input. Throttling and debouncing is to reduce server fetches to the absolute minimum while still providing satisfying user experience.

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

    what is your vscode theme?

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

    If you're reading this pause at 4:20 and look at the code, avoid redundant if statements, render the null being passed in the first ternary operator should be replaced with the render in the second ternary operator and completely remove the second ternary operator you don't need a redundant if statement like that.

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

    In React 18 they introduced useDeferredValue hook, you could use that instead of custom solution (which is absolutelly fine looking) if you already using newest version of React

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

      please share the official documentation on that hook, or you mean a custom hook....

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

      No they didn't, wtf?! I think you mean useDeferredValue which is similar but the same

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

      @@und0 yes I meant exactly useDeferredValue 😅 I will fix it in my commit, sorry

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

    I never had this problem cause when i started react, i also started using typescript

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

    I'd prefer doing !!showWinning && ( ) or TS, I don't like that much ternary operator with large blocks, it's difficult to read. We can algo extract the blocks to variables and then use the ternary op.

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

    Great video! Which theme do you use in VSC?

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

      I would like to know either

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

      Halcyon ^_^

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

      @@CoderOne Thanks! Keep going with this type of content

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

    Thanks a lot. I thinking about conditional rendering: IMHO the best is create function, like renderSomethink, and inside this function return some part of jsx in conditional case
    const renderSomethink = () => {
    if (flag) {
    return (aaa)
    }
    return bbb
    };
    return (
    {renderSomethink()}
    )
    ternary operators (?:) in jsx template it is terrible practice

  • @skylan6446
    @skylan6446 11 หลายเดือนก่อน +1

    It seems that use useRef for debounceValue instead of useState in useDebounce is much better, because it reduces the times of re-render

  • @thiagoleobons390
    @thiagoleobons390 11 หลายเดือนก่อน +1

    That's why everyone uses typescript. Boolean && is just fine, keep using it

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

    May i know the theme name?

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

    Great explanation. Thank u

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

    Related to first mistake
    Is better to convert inputvalue to boolean by !!
    or is it still not good solution?

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

      casting solves the problem if the type is number or unary of number and other types. Using TypeScript you can choose to cast only when the type can be number so that is the ultimate solution

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

      @@und0 Of course, but the variable is showWinning. Based on name it should be boolean adn it should be defined as boolean

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

    isn't just using foo && bar just fine, as even the mozilla docs says it's to check if it can be converted to false, and that it's a perfectly fine way to check if your variable or function even exists at call time

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

    For deboucing we could also use useDeferredValue. This will ensure high performance for larger data
    const [value, setValue] = useState("");
    const [data, setData] = useState([]);
    const deferredData = useDeferredValue(data);
    useEffect(() => {
    const id = setTimeout(() => fetch("/data").then(setData), 300);
    return () => clearTimeout(id)
    }, [value])
    return (
    setValue(e.target.value)} />

    );

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

      Experimental

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

      @@victormog No, it's not. It's stable since React 18.

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

      @@grenadier4702 Ok!

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

    hey man, I finally understand how forwardRef works!

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

    Thank you so much!

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

    I also much prefer the && condition over a ternary when you don't have a fallback. Just make sure to cast to a boolean first. If I see && I know I am conditionally rendering something. If I see a ternary I know I am conditionally rendering and I should expect a fallback template after. Not just null.

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

    Instead of {x ? a : null}{!x ? b : null} you can write {x ? a : b}. Instead of ref={ref} value={value} onChange={onChange} you can write {...{ ref, value, onChange }}. Just thought you might find that helpful.

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

    why can't we prepare a value first and the apply a conditional rendering (for example with !! or Boolean())

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

    Thank you... for qaulity content... debounce trick seems to be smooth.

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

    Regarding conditional rendering, I feel better readability is achieved by placing the relevant HTML into a separate component and giving it a "visible" attribute. That will remove ANY related JavaScript from within the calling JSX/HTML.

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

    3:50 put the bottom return as the false expression for the first ternary

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

    One blogger stated that using ternary operators is bad practice, while another recommended that conditional rendering should return false instead of null. It's great that I can merge both recommendations.
    However, it's unfortunate that the React documentation doesn't provide a comments answer on such small but useful tips.

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

      Blogger 😂

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

    Need more of this!

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

    I tend to distrust videos with titles like "don't do this" or "stop doing this". They often present personal and opinionated arguments, leading viewers to believe they're on a completely wrong path.

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

    does React Query handle the debouncing for you?

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

    Your debounce does not solve another issue with async on user input. You set the state on resolve and don't check if the resolve is relevant to the user input. The order the asyncs are resolved are not guaranteed to be in the order the user input triggered them, debounce mediate that somewhat but doesn't eliminate it. I am not allowed to post the link to the code but if you are interested I can give it a try (yt keeps just nuking the comment if I try posting my github account or gist).

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

    You just use '!!' To force null force coalescence.!!! For false.

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

    4:16 sorry sir, I can't agree this approach is more readable. Less prone to errors - sure, but more readable - not at all.

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

    Thanks 😊

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

    For the first one - why not just say showWinning = Boolean(someVar) it will be safe enought to conditionally render. Better than writing ? : and nulls in my oppinion.

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

      or use !! for converting to boolean

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

    Thanks 🙏👍

  • @user-rb9ch1el7c
    @user-rb9ch1el7c 7 หลายเดือนก่อน +1

    Denounce is good, but for truly senior devs, you simply cache every idempotent iteration of whatever request makes sense to cache... if not almost all... So, just stop using lo-dash, have some balls and venture into cache re-validation, you will be really happy about all the errors you will introduce, get fired and find a better job as a consequence. Thank me later. (LOL)

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

    I love how a lot of devs tends to search for convoluted solutions to problems TypeScript already solved years ago just for the novelty of not using it and look like a pro

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

    Why are we all using && and not ?? (null coalescing)?

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

      Two different use cases.
      X ?? Y translates to “return Y if X is null/undefined, otherwise return X” while X && Y translates to “return Y if X is truthy, otherwise return X.”
      He wants to return certain JSX only if the defined condition is truthy, basically meaning it has a value, but truthy does not just mean boolean “true.” This is the issue he speaks to in his first point, since everything that isn’t falsy is considered as truthy including non-zero numbers, non-empty strings, and “true” just to name a few.
      His solution works, but is much more easily solved by ensuring that your condition only returns a boolean, therefore limiting your truthiness scope to one possible value of “true” and preventing other non-boolean truthy values from showing instead.
      All in all, nullish coalescing (??) is more specific than logical AND (&&) since it’s only checking for null/undefined, but it is more commonly compared to logical OR (||) since it’s closer in functionality.

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

    Knowing / allowing `showWinning` to be anything but a boolean IS the bug. Not the fact that 0 renders `0`.
    Using a ternary just adds boilerplate code and isn't any clearer in my opinion.
    It's a little bit like writing:
    ```
    function isEven(value) {
    if (value % 2 === 0) {
    return true;
    } else {
    return false;
    }
    }
    ```
    rather than:
    ```
    const isEven = (value) => value % 2 === 0
    ```
    in my opinion.

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

    Why not use double ! to cast as a boolean? It's cleaner than a ternary.

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

    You even showed the solution to the one very rare case when condition && something fails that being a !! operator, why make the code less readable by an ugly ternary expression?

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

    Vs code theme?

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

    double negation (wich always results in an actual boolean) is way quicker and safer than a ternary :
    !!showWinning && ...

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

    clear and valuable, appreciated.

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

    why not just use the useeffect and grab the data once on mount

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

    TS solves the issue of the conditional variable being a non boolean type. Use TS. It's 2022, everyone should be using TS.

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

    First problem - just use TS!

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

    honestly, your problem with conditional rendering made me cringe so hard. Francisco Barros replied in a comment earlier !!x && component, if in doubt double bang and you are sweet. Unreal how you are peddling this tripe to juniors.

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

    interesting, thank you

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

    Why not (showWinning === true) && ?

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

    I'll keep this video on my heart... Thank you a Lot !!!

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

    Totally disagree. It really depends on the situation and what you want to check for. If you don't need to check for the 'else' condition, using tenary operator to have a null as the 'else' is just writing extra boilerplate code for the sake of writing it.
    In those situations it's better to just use &&

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

    Typescript saves !

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

    && with array is 0 when length is 0

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

    Amazing tips! Subscribed!

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

    Both && and ternary ARE conditional operators. You’re kinda wrong on that first one

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

    Tbh. I only watched for the first one. As it was a click bait. I agree that you should not use conditional rendering if you don't know what you are doing.. but other than that. And if you are really scared that you dont know what here is. you can use !! to convert it to boolean value

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

    Conditional rendering works fine (per CoderOne) if you actually get a boolean. So the true issue is type checking and type safety, which you get automatically with TypeScript and .tsx files instead of .jsx files. This is poor advice, ignoring a glaring problem (not using TypeScript) in favor of focusing on an easily resolved glitch that would never escape dev testing.

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

    You would expect the condition to actually return Boolean, so ternary operator is much less bug resilient. Just because in your case it shows what you want doesn’t mean it’s better. Btw ternary is to render one or the other thing, so why you made two conditions there? 😂 that looks so junior.

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

    Why did you do: showWinning ? this : null and !showWinning ? that : null, you should have done showWinning ? this : that. Conditional rendering one element with && is fine but ternary for if else is more appropriate (or use if/else if you want to be more verbose).

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

    Interesting content

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

    yeah. the actual problem isn’t the notation. It’s non-experienced coders that write no tests and walk into wonderland. 🎉 won’t continue after first one, obviously targets non-programmers

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

    Great tutorial, thank you!

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

    Lol. If you end up receiving a 0 number from the BE, when you expect a boolean, how is that a React FE issue

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

    That is why we should use TypeScript

  • @merloali
    @merloali 10 หลายเดือนก่อน +1

    wao, you vs**e looks like my neovim code editor, cool

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

    && easily solves by using TS :D

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

    also stop doing:
    1. useEffect cancer where logic is hidden in a chain of unreadable useEffect that could totally being done in a single async function
    2. application logic scatter around in the entire hierarchy of component instead of being centralize at the root of the feature

  • @js.1337
    @js.1337 ปีที่แล้ว +4

    for conditional rendering you could also just use `showWinning=== true && ()` or with a javascript typecheck `typeof showWinning === "boolean" && ()` ...

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

      U mad

  • @DM-pg4iv
    @DM-pg4iv 5 หลายเดือนก่อน

    Wwll your first mistake is using JS. Should use TS. It'll at least force it to be a bool type .

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

    2:10 "We all know 0 actually is the same as false" Wow...
    It is not the same, what you have probably wanted to say is that is is **falsy**.

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

    Topic 4 should have been to stop using javascript and to use typescript instead!

  • @user-ec7ne8rn5v
    @user-ec7ne8rn5v ปีที่แล้ว

    You can use !!showWinning and stay out of using not quite beautiful ? : null

  • @27sosite73
    @27sosite73 ปีที่แล้ว

    dubouncing starts at 4:45 not 3:37
    wtf?

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

    !!

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

    How about
    !!showWinning &&
    Will not show 0

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

    Just split the conditional render from the first part into it's own function pointing at sub components/functions.
    This video seems entirely driven by mistakes you've made and your hacks to solve them rather than best practices.

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

    That's why we have TypeScript.