3 ms·
Has anybody tried both Redux and Baobab? If so, do you think Redux is better and why? I'm using Baobab in a couple of projects and really liking it, but Redux f
by iliaznk 11y ago
Has anybody tried both Redux and Baobab? If so, do you think Redux is better and why? I'm using Baobab in a couple of projects and really liking it, but Redux from the tutorial I read seems to a bit too general to me. In Baobab you can subscribe components to their particular branches and only those components will re-render if the branches change.
In Redux, it seems to me, there's no way you can do it, or at least nothing is provided out of the box. Am i correct?
- Bromlife 11y agoI accomplish this in Redux with a simple: componentDidMount() { item = store.getState().get('item') // Get function from Immutable.js this.unsubscribe = store.subscribe(() => { let nowItem = store.getState().get('item'); if (item !== nowItem) { this.forceUpdate(); item = nowItem; } }); } Someone may have a better solution, but I think this works fine for my needs. The internet is full of people dissuading redux users from subscribing to actions.
- iliaznk 11y agoWhy would they dissuade? Is subscribing considered as anti-pattern?
- iliaznk 11y agoOh, I see, Redux also requires Immutable.js to work, correct?
- vkjv 11y agoReduce requires that functions be pure (no side effects). Immutable.js is one way to enforce this but ithe is not required. The examples mostly use things like the spread operator and Object.assign.
- acemarke 11y agoNope. Redux does require that your reducers update their state immutably, but does not care HOW you do so. You can do it with Object.assign() / _.extend(), array slicing, the object spread operator, Immutable.js, the React Immutability Helpers, or one of the umpteen libs that provide abstractions for immutable updates like dot-prop-immutable or object-immutable-path.
- danabramov 11y agoA better solution would be to use React Redux that does this and many other optimizations for you :-)
- CuriousSkeptic 11y agoYou can use reselect in combination with react-redux for a similar effect. Reselect declares a view of the state, and react-redux manages connecting the component to this view. I would say redux differs from baobab in not enforcing a state structure that mirrors the component tree. So it might be that if your state structure matches the component tree baobab is the simpler way, and redux is better if you need to decouple them. I'm using redux with a structure nothing like the component tree. I ended up keeping updates and the tree trivial and keep all logic related to how to present the data in selectors (Reselect). Ending up with a structure not unlike the reflux implementation i was migrating from ;) to isolate parts of the state i have all subtrees export getters and actions so that reselect selectors works with the root getter for the subtree.