Hacker News new | ask | show | jobs
by Bromlife 3765 days 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.
3 comments

A better solution would be to use React Redux that does this and many other optimizations for you :-)
Why would they dissuade? Is subscribing considered as anti-pattern?
Oh, I see, Redux also requires Immutable.js to work, correct?
Reduce 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.

Nope. 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.