防止在 redux 中触发子组件的选择器

prevent selectors of child components being triggerred in redux

如何防止每次父组件带来数据时更新我的​​子选择器?

选择器的行为是有道理的,我只是在寻找替代方法,更像是更简单的方法,然后使用 reselect(不允许在 redux 之外使用)。

gist

Reselect selectors are memoized, which means that if the selector's params are the same, the resulting props are the same object. Even if you can use reselect, you can memoized your selectors easily by implementing a memoization method, like the one from Addy Osmani's article:

function memoize( fn ) {
    return function () {
        var args = Array.prototype.slice.call(arguments),
            hash = "",
            i = args.length;
        currentArg = null;
        while (i--) {
            currentArg = args[i];
            hash += (currentArg === Object(currentArg)) ?
            JSON.stringify(currentArg) : currentArg;
            fn.memoize || (fn.memoize = {});
        }
        return (hash in fn.memoize) ? fn.memoize[hash] :
        fn.memoize[hash] = fn.apply(this, args);
    };
}

然后用它来创建选择器:

const selector = memoize((state) =>({
   alerts: selectors.alerts(state) 
}));