redux 中的选择器是什么? [英] What are selectors in redux?

查看:41
本文介绍了redux 中的选择器是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循这个然后在传奇中使用它,例如 这个:

Which is then used in the saga like this:

import { getUser } from '../reducers/selectors'

// load user unless it is cached
function* loadUser(login, requiredFields) {
  const user = yield select(getUser, login)
  if (!user || requiredFields.some(key => !user.hasOwnProperty(key))) {
    yield call(fetchUser, login)
  }
}

这个 getUser 减速器(它甚至是减速器)看起来与我通常期望的减速器看起来非常不同.

This getUser reducer (is it even a reducer) looks very different from what I would normally expect a reducer to look like.

谁能解释一下什么是选择器,getUser 是一个减速器,以及它如何与 redux-saga 配合?

Can anyone explain what a selector is and how getUser is a reducer and how it fits in with redux-saga?

推荐答案

getUser 不是reducer,它确实是一个选择器,也就是一个知道如何提取特定片段的函数来自商店的数据.

getUser is not a reducer, it is indeed a selector, that is, a function that knows how to extract a specific piece of data from the store.

选择器提供了一个额外的层,这样如果你改变了你的商店结构并且突然之间你的用户不再在state.entities.users 而不是在 state.users.objects.entities (或其他)然后你只需要更新 getUser 选择器而不是你的应用程序中的每个地方参考旧位置.

Selectors provide an additional layer such that if you altered your store structure and all of a sudden your users were no longer at state.entities.users but instead at state.users.objects.entities (or whatever) then you only need to update the getUser selector and not every place in your app where you were making a reference to the old location.

这使得它们在重构您的 Redux 商店时特别方便.

That makes them particularly handy when it comes to refactoring your Redux store.

这篇关于redux 中的选择器是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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