2 ms·
The problem I've had developing with React (I've just done a little, I'm still a beginner), is it sometimes seems like a minor change to the UI can require a ma
by jrochkind1 10y ago
The problem I've had developing with React (I've just done a little, I'm still a beginner), is it sometimes seems like a minor change to the UI can require a major change to the source, cascading through multiple components.
I think maybe this is reflected in this guide, such as the advice in the "Identify where your state should live" section (which they say is "often the most challenging part for newcomers to understand", which matches my experience), which suggests you should be looking at every single component involved and how they all relate to determine where the state should live.
This is very different from my previous (mostly OO, lately ruby) experience, where you really want to look at a component (in the broad sense, not neccesarily a view-layer, I just mean any object or 'piece') in strict isolation, identify what it's inputs and outputs need to be, and from that alone, in isolation, you know what it's API should be. And from this technique, you hope to wind up with components that that do not assume or depend on too much about their context of use and can be re-used and re-mixed in different ways without a rewrite.
I've found I've had to change my whole approach designing React -- when using my previous time-tested techniques, I would wind up putting props/state in the wrong place. I found that indeed I had to consider the entire graph when determining props/state. But that this has led me to situations where what my intuition would say would be relatively small changes to the UI create a cascading waterfall of changes I have to make to lots of and lots of components in the existing graph, as where state (including props) is kept needs to change, requiring cascading changes to many components in the previous graph.
Examples of such UI changes would be taking a UI widget/component that previously was only one one page and realizing it had to be re-used on another(s); moving a UI widget from one part of the page to another; taking a UI widget that previously just displayed something and realizing it needs to have an 'edit' capability too; taking something that showed up just once on the page and changing it to be say at both top and bottom of page. All things that my usual techniques and approaches from OO experience would have been able to accomodate no problem if I had designed the components well from the start, but that in React I'm finding result in needing to change where state is kept (or what things are state and what things are props), requiring cascading changes to many components.
Which is frustrating. I hoped I'd get better at avoiding this as I got better with React, and I'm sure that's true, but if even the advice from those most experienced says you can only determine state/props architecture by looking at the entire object graph possibly involved in that state....
I dunno. Comments from people more experienced with react very welcome.
- zxcvcxz 10y ago>it sometimes seems like a minor change to the UI can require a major change to the source, cascading through multiple components. >in React I'm finding result in needing to change where state is kept (or what things are state and what things are props), requiring cascading changes to many components. It sounds like you don't have a good understanding of state vs props. You should use state as little as possible and try to do as much as possible through props, passing them down from the parent components as necessary. When I create a react app I use the flux architecture, define a state on the top parent component and then add most of the other components as children to this parent component, passing down state as props, and adding listeners to update the components when the state of the parent changes. As far as moving elements around I mostly do that through CSS and wasn't aware react had much to do with positioning elements >taking a UI widget/component that previously was only one one page and realizing it had to be re-used on another(s) Import it into the component that renders the other page and use different props. If you define a lot of stuff in the state of the component then the component isn't as portable. >moving a UI widget from one part of the page to another; This just sounds like CSS and possibly markup to me. >taking a UI widget that previously just displayed something and realizing it needs to have an 'edit' capability too Not sure what you mean by this. Sounds like you could use more practice. https://github.com/uberVU/react-guide/blob/master/props-vs-state.md https://github.com/uberVU/react-guide/blob/master/props-vs-s...
- jrochkind1 10y ago> It sounds like you don't have a good understanding of state vs props. I think I've got a decent understanding, but I'm sure it could be better, and am heartened to think as it gets better these problems will go away. > You should use state as little as possible and try to do as much as possible through props, passing them down from the parent components as necessary. But yes, I've arrived at as little state and as many props as possible. The challenge for me here is that this ends up resulting in components that look to me (based on previous OO experience) like they have TERRIBLE API's, requiring way too many props. My instinct from OO is that a _smaller_ exposed API makes things more portable, but you suggest that "If you define a lot of stuff in the state of the component then the component isn't as portable." Nonetheless, I have moved toward more-props less-state, but.... ALSO this has meant for me that when I need to move or re-use a component from one part of the hieararchy to another, I often end up needing to completely rework the props of a BUNCH of other components, so the right prop can be passed down to my now-moved component -- that's what I mean by 'requiring cascading changes'.