4 ms·
I accomplish this in Redux with a simple: componentDidMount() { item = store.getState().get('item') // Get function from Immutable.js this
by Bromlife 11y ago
I 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 :-)