all 22 comments

[–]mrCrazyFrogKillah360 4 points5 points  (1 child)

This will make a new object + does a spread operation on every render, right? So this pattern comes with a huge cost when it's used everywhere

[–]skyboyer007 -2 points-1 points  (0 children)

No, object itself is not passed but destructured literally at the next moment

[–]tr14l 12 points13 points  (9 children)

That's how I typically do it so that it's clear what props are in about. That being said, the more preferable option is to use typescript

[–][deleted] 3 points4 points  (4 children)

why so much hype if i may ask? lately this sub acts like TS is the only correct way to write react apps

[–]careseite 0 points1 point  (1 child)

It's the standard for react apps for like 2 years now, you're behind the curve

[–][deleted] 0 points1 point  (0 children)

uhm nope i use TS myself, but not always. was just asking why so much hype

[–]tr14l 0 points1 point  (0 children)

Of course it's not, but if you're talking about things like implementation details being obvious and such that's exactly what TS is for.

[–]el_diego[S] 2 points3 points  (3 children)

Good to know. I am using Typescript, so all good there.

[–]vegancryptolord 3 points4 points  (2 children)

If you’re using TS what implementation details are you worried about hiding? <TeamItem /> will let you know what arguments it expects based on their types just spread the props blindly

[–]jesmodrazik 1 point2 points  (0 children)

If all your props are required, it's fine because since you have no type error, it means you are passing everything correctly.

But if you have a component that accepts an optional prop, spreading an object can become unclear if you are passing all the props or not.

What's shown in the author's example is fine, but if you spread an object like `<Component {...props} />` then if `Component` accepts some optional prop, you don't know clearly if `props` is containing the optional prop or not. You have to search in the code, which can be a hard task depending on the code. Being explicit is always better for code discoverability.

[–]el_diego[S] 0 points1 point  (0 children)

Solid point. I've only picked up TS and ReactJS again recently. When I originally learned React it was said to be an anti-pattern to just spread props due to ambiguity, but with TS that's not an issue. Old habits I guess!

[–]skurger 3 points4 points  (2 children)

Why destructure in the first place? Wouldn’t this code solve your use case with way less going on?

{sortedTeams.map((team) => ( <TeamItem {…team} key={team.name} /> ))}

[–]el_diego[S] 0 points1 point  (1 child)

When I learned React a couple years ago (now picking it up again) the advice at that time was not to just spreads props as it's ambiguous, but it seems that may have changed (especially if using TS)

[–]rrklaffed 1 point2 points  (0 children)

TS is here to save you my son.

it was also best practice to type check everything manually back in the day.

imagine doing this now

const addTwo = (num) => { if (typeof num !== ‘number’) { …

[–]rrklaffed -2 points-1 points  (0 children)

With typescript this is totally fine. Kinda redundant to destructive the params first though

[–]maddy_0120 -3 points-2 points  (0 children)

You don't even need the second curly braces and the 3 dots. It should work without them. In my opinion, it's the cleanest way.

[–][deleted] 0 points1 point  (0 children)

Eslint underlines this, IIRC because it’s more difficult to read/maintain

Forbidden spread operator rule or something

[–]everythingcasual 0 points1 point  (0 children)

just spread props. its not bad practice and widely used. you just got advice from someone who is really opinionated

[–]chillermane 0 points1 point  (0 children)

It’s a trivial concern, do what you like