3 ms·
> Components that expect “onlyChild” > Some React components have an invariant: “Invariant Violation: onlyChild must be passed a children with exactly one chil
by rco8786 11y ago
> Components that expect “onlyChild”
> Some React components have an invariant: “Invariant Violation: onlyChild must be passed a children with exactly one child”. Which means this is an error:
I'm surprise the author called this out as a negative of React while saying the compile time type safety is a positive of Elm.
If I have something that expects one child, I expect one child, not an array that has 1 element.
- Scriptor 11y agoAgreed, the benefits of type safety are often thrown around but this is sort of what type safety looks like. Yes, as the author says it makes things "annoying to develop". This is because the developer now has to read the docs and be aware of this invariant for the particular component they're using. They'll go, "Damn, guess I can't use a list here," and fix their code. However, let's say later down the line a new engineer comes in and is tasked with a bug fix. If they're using Cycle.js and see an array, they might think, "Oh, I can easily solve this by adding another element to this one-element array." Without any invariants they'll run into subtler bugs and errors trying to figure out what went wrong. But with the invariant, if they tried to jam an array there they would immediately get an error and know what's wrong. This isn't something that will crop up in a simple one-day project like the author was creating with his coworker. It's instead a great example of how a little annoyance can make future maintainability way easier.