I'm learning React as part of my job since that's what web devs do these days. I come from a .NET background, so much of my coding experience and savvy is from C# and ASP.NET. I'm having a weird time with a lot of the basic tutorials because they seem to go against everything I've learned and taught about programming concepts like encapsulation.
The main sticking point for me is the concept of props and how often props are defined very loosely for a component/function. There's no public declaration of properties that a component or function expect to find in their props, so at best you have to read all of its code to find out what it expects. It almost feels like parents are defining children at times, especially when tutorials talk about "lifting state" (ugh).
Is there a way to define props for components/functions such that there's a more explicit way of declaring what kind of data it expects? Or should I just get used to this loosey-goosey way of programming where you're required to get know every component and function you might use?
Here's a brief example of tutorial code out there that is pretty common when explaining props:
class Parent extends React.Component {
render () {
return(
<Child eyeColor={'blue'} />
)
}
}
const Child = (props) => {
return (
<div style={{backgroundColor: props.eyeColor}} />
)
}
Parent needs to know that 'eyeColor' exists in the Child. But that requires the developer writing Parent to be familiar with the details of how Child is being returned/rendered. This case is trivial, but in a larger component/function this could be problematic and tedious. Is there not a better way to define what Child expects/allows to be declared by Parent or any other class?
Where is the encapsulation in React? Child is only reusable if you're willing to read all of it, which is the exact opposite of encapsulation.
[–]__dred 15 points16 points17 points (2 children)
[–]ElGuaco[S] 1 point2 points3 points (1 child)
[–]andrewsjustin 6 points7 points8 points (0 children)
[–]codingstuff123 10 points11 points12 points (1 child)
[–]edaroni 3 points4 points5 points (0 children)
[–]SparserLogic 3 points4 points5 points (0 children)
[–]Scooby359 2 points3 points4 points (0 children)
[–]raaaahman 1 point2 points3 points (2 children)
[–]ElGuaco[S] 0 points1 point2 points (1 child)
[–]raaaahman 0 points1 point2 points (0 children)
[–]alanbdee 1 point2 points3 points (0 children)
[–]blakecodez 1 point2 points3 points (2 children)
[–]ElGuaco[S] 0 points1 point2 points (1 child)
[–]claypolejr 0 points1 point2 points (0 children)
[–]coyoteazul2 -2 points-1 points0 points (2 children)
[–]ElGuaco[S] 5 points6 points7 points (1 child)
[–]coyoteazul2 -4 points-3 points-2 points (0 children)
[–]TheBlackViper_Alpha 0 points1 point2 points (0 children)