防止在 redux 中触发子组件的选择器 [英] prevent selectors of child components being triggerred in redux

查看:42
本文介绍了防止在 redux 中触发子组件的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can i prevent my child selectors from being updated each time the parent component bring data?

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

selectors behaviour make sense i am just looking for alternative approaches more like simpler approach then using reselect( not allowed use beyond redux).

要点

推荐答案

Reselect 选择器是 memoized,这意味着如果选择器的参数相同,生成的 props 是同一个对象.即使您可以使用重新选择,您也可以通过实现一种记忆方法轻松记忆选择器,例如 Addy Osmani 的文章:

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);
    };
}

然后使用它来创建选择器:

And then use it for creating the selectors:

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

这篇关于防止在 redux 中触发子组件的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆