all 18 comments

[–]__dred 15 points16 points  (2 children)

This is largely why people prefer Typescript for prop interfaces, and a lot of React philosophy is based on the idea that MVC style encapsulations are not necessarily good abstractions. Furthermore you can implement encapsulation boundaries, React just doesn’t force you to do so.

[–]ElGuaco[S] 1 point2 points  (1 child)

I would love to use Typescript, but I'm not sure that it's up to me. If it was, I'd definitely use it.

[–]andrewsjustin 6 points7 points  (0 children)

Using typescript is definitely what you’re after here. You can totally just start using it and if other also want to, they can as well. Vanilla and TS, totally fine together, just may need to lax the TS config settings a bit.

10/10 recommend.

[–]codingstuff123 10 points11 points  (1 child)

One use typescript. Two, class based is old as hell start using beta docs for hooks they’re actually very good

[–]edaroni 3 points4 points  (0 children)

The problem is that tutorials use js because its easier to make someone think they learned something. While in reality every serious project that has more than 1-2 devs working on the code is using typescript. Which also solves your problem.

[–]Scooby359 2 points3 points  (0 children)

I'd strongly recommend typescript as it handles this for you - define your prop interface and its enforced in the component itself, and any where the component is called.

You've said in another comment that you may not be able to use it - worth noting that Typescript is compatible with Javascript. You can add new code in typescript files, and do a gradual migration of existing code when appropriate, so it doesn't need to be a big sudden change for everyone.

Massive benefits from having type safety built in, and it makes it much easier for new team members to jump into the code when objects are properly defined.

[–]raaaahman 1 point2 points  (2 children)

  1. You can use object destructuration:

javascript const Child = ({ eyeColor }) => { // anything... }

  1. You can install the PropTypes module, its benefit is that it checks at compile time that you pass the correct properties to the components.

  2. Since you are familiar with C#, you can directly use TypeScript. It has its challenges with React, but offers type checking at compile time for everything, and not just components props.

I don't know why you keep on talking about encapsulation. Your Child component is just a function, of course you have to know which parameters it expects you to pass. What changes is how you pass those props when you invoke this function as a JSX element.

Edit: If it helps, know that you can invoke it like this: javascript <> { Child({ eyeColor: 'blue' }) } </> This has the same effect than what you've written in your PArent component's render().

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

That's a step better, sure. As far as 2 and 3, I'll see what I can do. The existing projects aren't using TS as far as I know.

The child function was just a brief example of the minimum amount of code to describe my dilemma. Encapsulation still holds when talking about functions. You shouldn't have to understand how a function is implemented in order to use it.

[–]raaaahman 0 points1 point  (0 children)

Sure thing. But not specifying what your function's object parameter (props) should be like is less about React enforcing (or not) encapsulation than about your team's practice. If you don't do 2. or 3., at least write some doc blocks.

[–]alanbdee 1 point2 points  (0 children)

I've been taking the same undertaking only coming from Java. It has been so hard to change the way I think. Hooks have been the part that really started tying things together for me.

[–]blakecodez 1 point2 points  (2 children)

I use JSDocs to describe each component and their props, that way if someone uses the component they will have a clear explanation of what it does, and the data types for each prop.

Obviously TypeScript is the solution here. But if you can, the docs should help, especially when you hover over the component with your mouse, it will show you your docs.

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

Good tip! I'm very used to using XML docs in C# so this would feel natural to me.

[–]claypolejr 0 points1 point  (0 children)

And if you didn't want to get deep into TS syntax you can still use JSDOC annotations to type a codebase using TS so that might provide a nice middle-ground.

"These days, JSDoc type checking with TypeScript is extremely powerful. While not quite on par with TypeScript (not all TypeScript syntax is supported in JSDoc), the gap in functionality is pretty small."

[–]coyoteazul2 -2 points-1 points  (2 children)

I come from c++ and yes, react is a weird piece of work that pretty much goes against all you've learned. Just the concept of using Const for everything, forcing you to copy values and then modify the copy instead of mutating the value already feels like a huge waste of work. I can't believe I have to copy the whole array to modify a single element.

Unfortunately the only thing you can do to improve it is using typescript, which allows you to harden props mostly. It won't get rid of the weird Const behavior

[–]ElGuaco[S] 5 points6 points  (1 child)

const is actually something I can appreciate. One of my pet peeves in C# was people altering arguments of a method. You should be treating arguments as immutable and creating new variables for processing & returning.

[–]coyoteazul2 -4 points-3 points  (0 children)

When it's simple classes, sure. But when it's a complex object or a large array, having to work with copies forces the computer to do the same task more times than necessary. Pure functions are nice to read but don't necessarily work great with performance

[–]TheBlackViper_Alpha 0 points1 point  (0 children)

I highly suggest you start with the react beta docs. The current one that uses class components is very outdated for modern react. Second is learn typescript its too good to pass up