简体   繁体   English

为什么要返回默认大小写而不是在Redux的reducer中抛出?

[英]Why return default case instead of throw in Redux's reducer?

I have read a lot of Redux tutorials, all of the tutorial return default case instead of throw, they write something like this: 我已经阅读了很多Redux教程,所有教程都返回默认大小写,而不是抛出异常,它们编写如下内容:

const counter = (state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
};

Why not throw an error since we already know the developer must've been dispatched unexisting action type? 既然我们已经知道必须为开发人员分配不存在的操作类型,为什么不抛出错误呢? like this: 像这样:

const counter = (state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      throw new Error(`Unknow action type: ${action.type}`)
  }
};

Why not throw? 为什么不扔?

In case of Redux, you might have multiple reducers which you combine using combineReducers to form a single store. 在Redux的情况下,您可能有多个reducers,您可以使用combineReducers组合在一起以形成一个商店。 When you dispatch an action, it is dispatched to all the reducers and only to the intended one. 当您分派一个动作时,它会分派给所有的reducer,而只会分派给预期的那一个。 Hence you would have a default case where you return the current state. 因此,在默认情况下,您将返回当前状态。

And while the action is being dispatched to all the reducers, only the one that has to process the action listens to it and updates the state. 并且,当将动作分派给所有的reducer时,只有那些必须处理该动作的人会监听它并更新状态。 This may lead to an action being dispatched but not process by any reducer but would certainly not cause an issue and will never happen unless you forgot to write this code. 这可能会导致某个动作被分派,但没有任何减速器进行处理,但是肯定不会引起问题,除非您忘记编写此代码,否则绝对不会发生。

However if you use a single reducer without combining them which is often a case with useReducer hook, you can throw an error if no matching action is being dispatched. 但是,如果使用单个reducer而不将它们组合在一起(在useReducer钩子中经常发生这种情况),如果没有调度匹配的动作,则可能引发错误。

When creating reducers, the objective is not to get back an error in lieu of our new state, the objective is to return the existing state if no new state is available. 创建减速器时,目的不是要取回错误来代替我们的新状态,而是要在没有新状态可用时返回现有状态。

const hiringProcess = (oldListOfCandidates, action) => {
  if (action.type === 'CREATE_CANDIDATE') {
    // we care about this action
    return [...oldListOfCandidates, action.payload];
  }
   // we don't care about this action
}

So with ES2015 syntax, I have taken an array of oldListOfCandidates and add them to a brand new array and then add in a new record of action.payload . 因此,使用ES2015语法,我获取了一个oldListOfCandidates数组,并将它们添加到全新的数组中,然后添加了一个新的action.payload记录。

Anyway, your question is not about key interpolation syntax but why return state; 无论如何,您的问题不是关于键插值语法,而是为什么要return state; instead of throwing an error. 而不是抛出错误。

We don't care about getting back an error, for the purposes of reducers and what the overall redux architecture is supposed to be doing for us, what we want to return is the existing data or state if we have no new data, that's the whole point of these reducers. 为了简化器的目的以及整个redux体系结构应该为我们做的事情,我们不关心返回错误,我们要返回的是现有数据或state如果没有新数据,那就是这些减速器的全部要点。

const hiringProcess = (oldListOfCandidates, action) => {
  if (action.type === 'CREATE_CANDIDATE') {
    // we care about this action
    return [...oldListOfCandidates, action.payload];
  }
   // we don't care about this action
   return oldListOfCandidates;
}

So if there is no new candidate to add to state then we don't need or want an error that explains why did not update state we just want to return state; 因此,如果没有要添加到state新候选者,那么我们就不需要或想要一个错误来解释为什么不更新state我们只想return state; that we already have. 我们已经拥有的。

And as you already know the first time the reducer gets called, it will receive the value of undefined and we need to default the value of the first argument for state only like so: 正如您已经知道的那样,reducer第一次被调用时,它将收到undefined的值,我们只需要像这样默认将state的第一个参数的值默认为:

const hiringProcess = (oldListOfCandidates = [], action) => {
  if (action.type === 'CREATE_CANDIDATE') {
    // we care about this action
    return [...oldListOfCandidates, action.payload];
  }
   // we don't care about this action
   return oldListOfCandidates;
}

I say for state only because I have done work at shops where they had defaulted the value of the action object and could not explain to me why they did this and at the same time did not bother to correct it, so don't do that its not necessary. 我之所以说是出于state是因为我在商店里做过工作,他们没有默认action对象的值,并且无法向我解释为什么这样做并且同时没有费心去纠正它,所以不要那样做。这不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM