4 ms·
People mainly use Redux because they don't understand that React isn't Angular. React is meant to care only about UI state. Your application state should be kep
by pcstl 8y ago
People mainly use Redux because they don't understand that React isn't Angular. React is meant to care only about UI state. Your application state should be kept separately, and React should just be a function from app state to UI.
Redux is what you get when people try to use React as a "full-app" framework instead of what it's meant to be: a view layer.
- WA 8y agoSo how do you manage your application state? I‘m curious, because a search for such a phrase always brings up Redux, Thunks and Saga.
- pcstl 8y agoYou keep it out of React, manage it however you want to and is most convenient for your application's data model, and pass in the parts that are relevant to UI as props to React's root.
- kalkut 8y agoYou can make any suitable variation of the following pattern. You build a DataSource class/prototype with methods such as : - fetch - addChangeListener - removeChangeListener You extend this class for each data type : class CommentsData extends DataSource { // Your custom data source with your data treatment and data getters } And you use the Higher-Order Component pattern: https://reactjs.org/docs/higher-order-components.html https://reactjs.org/docs/higher-order-components.html
- boubiyeah 8y agoThat's just a more Object Oriented Redux (or less, if we consider OOP is originally message passing), and probably without all the connect optimizations. I'm not a fan of Redux either, but I fail to see what problems this approach solves.
- mosdl 8y agoWhat connect optimizations? Redux is quite slow.
- acemarke 8y ago`connect` does a _lot_ of work to ensure that your own components only re-render when necessary: - It checks to see if the root Redux state is different than last time, and bails out if it's the same reference under the assumption that nothing changed - It checks to see if the return values from your `mapState` function are shallow-equality different than last time, and again bails out if they're the same - The handling for `mapState` and `mapDispatch` is heavily memoized to cut down on unneeded work. If you have specific performance concerns with a Redux app, please let me know - I'd be happy to offer advice and point you to possible solutions. But, a blanket statement that "Redux is quite slow" is meaningless without context for what's happening.
- mosdl 8y agoIn large apps mapStateToProps gets called when things my component doesn't care about change - its a simple downside to having one global store as a design. We now speed things up buy removing redux and just using events with multiple stores anyone can access - we can easily tune the system and don't have to rely on magic that can never be as efficient. Plus the syntax is much cleaner.
- kalkut 8y agoDispatch is another big culprit. I have exactly the same experience and implemented the same solution. An ex-colleague also did the same thing. We both worked on collaborative webapps but in different companies at the time so performance gain was very important. I still use Redux on some projects but I definitely start the new ones without thinking about it.
- acemarke 8y ago
- Vinnl 8y ago> Your application state should be kept separately, Is Redux not exactly that?
- pcstl 8y agoIt can be used for that, but it quickly becomes cumbersome. Redux, in my opinion, should be used for complex UI state in applications with complex interfaces where interactions with one part of the UI can change very different parts ("spooky action at a distance", so to say). But it shouldn't be used to keep all your application's state. Your application's state should live outside of React, and be passed in as the root's props.
- Vinnl 8y agoI agree w.r.t. what you should Redux for. That said, if you store application state in Redux, is that nout "outside of React", passing it in as props? (Though not necessarily the root's props.)
- d0100 8y agoI think the opposite. Redux should have your application state, and any UI state should be in pure React (portals, context, etc). Redux -> app data React -> UI
- shados 8y ago> People mainly use Redux because they don't understand that React isn't Angular Ironically, Redux came out around the time Angular popped. While not everyone, a significant portion of early adopters came in because they were rejecting the Angular 1.X way of handling state, and Redux was the polar opposite.