6 ms·
My first project with React was a mess, mostly because of Redux. Not because it's bad, but because the lead dev was adamant about using something Flux like. Fir
by k__ 9y ago
My first project with React was a mess, mostly because of Redux. Not because it's bad, but because the lead dev was adamant about using something Flux like. First we had Flummox, than he rewrote everything in a week with Redux, that was 2 years ago, before Redux had sane async helpers.
In my current project (where I'm the lead, haha) I'm trying to got state/props all the way.
I think for most apps it's more than enough AND it's easier to get started for new devs.
React is really easy. I mean coming from Ember and ExtJS, its API is a walk in the park and you can get really far without extra state management.
One thing that is important for this approach, minimize nesting.
You don't want to pass down everything from the app, to the screen, to the list, to the item. etc.
Instead of doing:
<List items={items} />
do it like that
<List>
{items.map(i => <Item item={i}/>)}
</List>
No nesting of screens (top-level components), no (hidden) nesting of components. This may seem kinda strange, because the first example has no dependency to the Item component, but it gives the component that uses the List direct access, which simplifies props-passing immensely.
This doesn't work for all apps, but it's a good starting point.
I ended up with 2 folders, screens and components, that are both simply flat lists of component files.
- lilbobbytables 9y agoI partially blame every tutorial and contrived example of how you should use Redux. When first starting with React & Redux I spent some time thinking of where and how I should use Redux. Being that I was new to both, I didn't have any ingrained best practices, and it was hard to find any best practices around when to use redux. Every example seemed to use it for everything, which seemed insane to me in a real web app. I could see that the size and clutter would grow terribly, and for no obvious benefit. Your example above is similar to how many component libraries work (where you need to give the user flexibility). In a dashboard app, I'll make good use of Redux because there are often sets of data you revisit in different ways. In a more directory-like site I've recently built I use it to keep track of the logged in user information and high level information that dictates your experience (like user location), but nothing else.
- k__ 9y agoI try to keep it as simple as possible. But for complex dashboards I would probably use some observable based solution.
- peruvian 9y ago> I partially blame every tutorial and contrived example of how you should use Redux. Partially? No. The culture of side projects/GitHub/blog post had led people to over engineer their learning materials and side projects to look as employable as possible. Why make a todo list with just React when you can use Redux in it too and put it in your resume?
- k__ 9y agoHe he true. When I do a side project I often want to do fancy stuff and blog about it. When I get paid for my work, I try to keep it simple, because I fear I could end up with too much intermingled stuff I won't understand anymore after a few months.
- softawre 9y agoWanting to do fancy stuff for a side project is understandable. I'd prefer my team do that, so they can learn new stuff - and only bring the techniques/tools that actually work to paid projects.
- acemarke 9y agoFor what it's worth, my links list has a very large section of articles on Redux architecture and best practices: https://github.com/markerikson/react-redux-links/blob/master/redux-architecture.md https://github.com/markerikson/react-redux-links/blob/master... . Mapbox's post on "Redux for state management in large web apps" is particularly good: https://www.mapbox.com/blog/redux-for-state-management-in-large-web-apps/ https://www.mapbox.com/blog/redux-for-state-management-in-la... .
- Kiro 9y agoHow will you solve two components deep down different branches that share state?
- k__ 9y agofor shared state I used localStorage/AsyncStorage and added an event emitter to it.
- laurent123456 9y agoIn this kind of setup, I'm wondering how you handle events that happen outside of the component? Let's say you have a background function, created with setTimeInterval, which after some time update an item. With Redux, you could dispatch an action UPDATE_ITEM, which would modify the items, and in turn update the connected component. However, without Redux how is your component going to know about this item update and update the list? Do you use some kind of event listener/dispatcher?
- badthingfactory 9y agoYou have a root component. Run the interval within that component where you have access to state.
- bendavis381 9y agoThat's essentially redux: root state.
- badthingfactory 9y agoIf that was "essentially redux", redux would have no reason for existence.
- RussianCow 9y agoExcept for the fact that it drastically lowers the amount of manual props passing you have to do, and allows you to achieve much better separation of concerns. It is essentially Redux, only much worse the more complex your app is.
- badthingfactory 9y ago> It is essentially Redux, only much worse the more complex your app is. so... not at all like Redux then. C# is essentially Python except it has different syntax and static typing.
- acemarke 9y agoThat approach may minimize props passing, but it also is going to cut down on chances to use `shouldComponentUpdate` to avoid unnecessary re-renders and improve performance. Depending on your app, that might not be an issue, but it's something to be aware of.
- Androider 9y agoIt's exactly the opposite. You want to make your props as specific to the component as possible to minimize re-renders and pass-through of props causes unnecessary work. <List items={items} /> will re-render (or at least completely unnecessarily re-evaluate shouldComponentUpdate) the List itself whenever any item has changed. <Item item={i} /> with a shouldComponentUpdate or better yet React.PureComponent will only re-render the single changed item.
- acemarke 9y agoI think we're sorta saying the same thing, but in different ways. If you have that separate `<List>` parent component, you can apply `sCU` there so that it only re-renders when some of the items have changed, and not when other parts of the application update. If you don't have the `<List>`, the parent component will always re-render and always at least start the render lifecycle process for the `<Item>` components, but they can also implement `sCU` to skip the actual re-render. The most optimized approach is to have normalized data in Redux. The parent component should retrieve an array of item IDs in its `mapState` so that it only re-renders when an item is added, removed, or reordered, and render children like `<Item id={itemId} />`. The `Item` components should be connected as well, and use their `mapState` functions to look up the corresponding item from the Redux state by ID. That way _they_ will only re-render when their specific item has changed.
- Androider 9y agoAlso, when you do use Redux, as in your nesting example don't pass props through components that don't have any need for them except they propagate the prop to someone else. It's absolutely fine to connect() a component deeper in the hierarchy. Passing everything down is an anti-pattern with Redux, separation of concerns still applies and the props of a component defines the API, don't clutter that API with unrelated things. Components only receiving props that they themselves need has kept things clean in our massive codebase, and allows you to effortlessly move things about and easily re-use components wherever it makes sense.
- acemarke 9y agoYep, absolutely. See the Redux FAQ entry on "connecting multiple components" for more info [0] . I also discussed how connecting more components affects performance in my blog post "Practical Redux, Part 6 - Connected Lists, Forms, and Performance" [1], and there's many additional articles that discuss connected components and performance concerns as well [2]. [0] http://redux.js.org/docs/faq/ReactRedux.html#react-multiple-components http://redux.js.org/docs/faq/ReactRedux.html#react-multiple-... [1] http://blog.isquaredsoftware.com/2017/01/practical-redux-part-6-connected-lists-forms-and-performance/ http://blog.isquaredsoftware.com/2017/01/practical-redux-par... [2] https://github.com/markerikson/react-redux-links/blob/master/react-performance.md#redux-performance https://github.com/markerikson/react-redux-links/blob/master...
- prance 9y agoI don't agree with the view that a component doesn't need a prop "itself" if it "only" passes it on to a child component. That child component is a part of the parent. If one decided that e.g. the child is too simple to warrant to be its own component and inlined its render() content in the parent instead, suddenly its used props would be needed by the parent "itself". Another thing to consider is that one tightly couples components to stores when connecting. So reusability of these components is hampered.
- pedalpete 9y agoIf was my understanding that if a component has a prop which changes, even if that prop is passed down the tree, the component will still update. This is one way I use redux, the connector watches for changes in state and then updates the component directly, rather than passing props down the tree.
- Y7ZCQtNo39 9y agoI've been doing React for a while now and I've ended up with the same structure: 2 main folders. One of them in the base components, which are in folders grouped by their corresponding REST service endpoint (so if you have 'Foo' and 'Bar' db tables, and API's, then I have 'Foo' and 'Bar' folders inside my 'components' folder. The other folder is my views. It's more of the integration layer for all of my components. Since views typically correspond to a page route, that's where most of my react-router stuff is. It also is where I call the react-redux 'connect' HOC, to get data that is fed down into the components. So far it's working pretty well for me. I think it works great for a medium-size type of app.
- rednotebook 9y agobetter yet - don't create functions, especially inside a loop, in your render function, getItem = i => <Item item={i}/> ... <List> {items.map(getItem)} </List>