News for hackers. Stuff that matters.
Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by _pdp_ on Thursday August 31, 2017 @03:30AM (#15138741)
In our shop where we have built many high-performant, and pretty complex (various types of security tools and all that) react apps, the state is managed at the top-most component and it is passed down to all other components as props. It works great and it is functional all the way.
Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by thenewestkid on Thursday August 31, 2017 @03:43AM (#15138793)
Passed around local state vs global state is indeed an interesting topic. Honestly I think both are nice in good hands.

The library I made supports both of them, but I did not mention local state management for the sake of simplicity. You can find it here: https://github.com/solkimicreb/react-easy-state#easycomp (it will take 1 min to read).

Doesn't it get clumsy to pass state as props to deeply nested comps?

Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by girvo on Thursday August 31, 2017 @03:39AM (#15138771)
Yeah, especially if you combine that idea with Flow/TypeScript or Reason. In fact that's why Redux isn't really used in Reason very much, with algorithmic data types, exhaustive pattern matching and all the power of OCaml you don't need it!

I enjoy the simplicity of it so much that I've now entirely adopted Elm -- type safety, super simple but powerful, and completely based on uni directional data flow with a compiler that is the most helpful I've ever used all wrapped up in the same State -> View function hierarchy that I adore in React.

Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by uryga on Thursday August 31, 2017 @08:24AM (#15139775)
You probably meant "algebraic data types" :)
Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by girvo on Thursday August 31, 2017 @09:03PM (#15145353)
I most definitely did :) remind me not to type while still half asleep!
Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by arc_of_descent on Thursday August 31, 2017 @03:37AM (#15138763)
So you pass the entire state object, however large it might get to all child components?
Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by girvo on Thursday August 31, 2017 @03:40AM (#15138783)
For child components that don't need the entire object, leverage destructuring assignment and it becomes a lot nicer than it sounds!
Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by mhd on Thursday August 31, 2017 @04:37AM (#15139006)
That works well for leaves of the component tree, but not in deeply nested hierarchies. You never now whether some subcomponent in your <NavBar> requires state.serverInfo.frobnicationCount...
Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by realharo on Thursday August 31, 2017 @04:15AM (#15138922)
So how do you deal with UI containers that don't really care about most of the state, but still need to pass it on to their child components?

It sounds like tracking this by hand ("what is needed where") could get messy, though I don't have experience with this approach.

Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by jcelerier on Thursday August 31, 2017 @04:51AM (#15139053)
> So how do you deal with UI containers that don't really care about most of the state, but still need to pass it on to their child components?

That's more or less the questions that they were asking in 1995 when they made the design patterns and conceived dependency injection.

Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by arc_of_descent on Thursday August 31, 2017 @04:24AM (#15138958)
Its turtles all the way down!
Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by shados on Thursday August 31, 2017 @11:21AM (#15140918)
Someone else beat me to the joke of "turtles all the way down", but that's mostly it.

I'll first refer to this Twitter thread: https://twitter.com/JEG2/status/902534133427134465

Now, the reasoning is: if you pass everything down, then things get interesting to reason about. Given a state to a component, it will always render the same way (give or take, local state and all). That is only true if you reason about children components as implementation details of the parent. That is, if a parent component renders a child one, and the child needs a prop, the parent should have that prop too.

Otherwise, you could render the parent 6 times with the same props and get 6 times a different result based on some undefined (at the parent level) magical global side effect.

Doing it this way makes your component tree essentially fractal: at any point you could detach an arbitrary component, put it in a library, and use it in another app: there's no assumption. Any part of the app is in itself a small app.

Now, the main issues is the boilerplate of typing it all out (which is minimized if you use a single "model" object at every level and either pass it as a whole, or a smaller part of it, to any child): if you use regular props for every value, you'll have top level components with 100+ props.

The other issue is maintenance. What happens when you move things around. That part really begs for a type system. Use Flow or TypeScript, and just let the compiler bark at you. When the compiler is done barking, you're done fixing, and you have a full statically checked view of your model dependencies. That's awesome!

The rest is mostly cultural. At which point does a developer throw their arm in the air and say "I don't want to do this!". A few months back, Jordan Walke did an Ask Me Anything on Discord where he said he had a pretty high tolerance for passing stuff down manually. I assume that explains some design decisions in React (eg: like how context was not a first class citizen earlier on), but I don't want to put words in his mouth.

For an example of this concept, look at Elm, where that's the primary way to build things. A fully fractal architecture where models come from the top and get passed all the way down to all the turtles. The way you build your model changes a bit (in apps Ive seen, it's more of a mirror of the component hierarchy, and less of a conceptual, normalized model), but boilerplate isn't that bad. Higher than the typical React/Redux dev will tolerate, which itself is higher than what most web devs will...but people who do it generally end up with very high quality apps that are trivial to maintain.

Re:The Simplest React Stack – Part 1: Bootstrapping and Stat... by davedx on Thursday August 31, 2017 @06:37AM (#15139386)
Yup! This is my approach when developing side projects. Keep everything explicit.