简体   繁体   English

React Redux体系结构:在哪里进行数据组织-操作或存储或组件?

[英]React redux architecture: where to do data organization - action or store or component?

I'm learning to build a pokemone app that hits the pokemon api , which gives you a lot more info then you need. 我正在学习构建一个可以播放pokemon api的pokemone应用,它可以为您提供所需的更多信息。 I'm having trouble figuring out where I should put my code that sifts out the only info I need for my app. 我在弄清楚应该在哪里放置我的代码以筛选出我的应用所需的唯一信息时遇到了麻烦。

For example, fetching a pokemon's type will return you a type object that looks like this: 例如,获取口袋妖怪的类型将返回一个如下所示的type对象:

{pokemon: [...], weakAgainst: [...]}

If this is what is the data that I'm getting back from my ajax request, and I only want the pokemon array, should 如果这是我从ajax请求中获取的数据,而我只想要pokemon数组,应该

  1. I sift this out in the success of the ajax call that is in my actionsCreators.js and then return that to be dispatched to the store? 我在actionsCreators.js中成功进行了ajax调用后,对此进行了筛选,然后将其返回给分派到商店? or 要么

  2. dispatch all of the data type object to my reducer and then in my reducer.js file sift out the pokemon array and copy it to my state? 将所有数据类型对象分派到我的reducer,然后在我的reducer.js文件中筛选出pokemon数组并将其复制到我的状态? or 要么

  3. copy the whole data object to my state and then sift out the pokemone array in my component's props (that was passed down from mapStateToProps)? 复制整个数据对象到我的状态,然后筛选出我组件的props中的pokemone数组(从mapStateToProps传递过来)?

I'm thinking it should be in the reducer? 我想应该在减速器中? Because it's job is to reduce data to what you need and move it on to the state? 因为它的工作是将数据减少所需的数据并将其转移到状态?

I am of the mind that you should hand the reducer what it needs, if you have the means. 我的想法是,如果有能力的话,应该将减速器交给需要的东西。 Inevitably, you will need to massage and manipulate data in the reducer, but if you can minimize it, I would. 不可避免地,您将需要处理和操作化简器中的数据,但是如果可以将其最小化,那我会。 Your reducer should be a pure function -- no side effects, the reducers always returns same output given same input etc.. I like to reduce "side effects" by giving the reducer exactly what it needs, especially since the data you need to handoff is so nicely handed to you.. ala. 您的reducer应该是一个纯函数-没有副作用,reduceers总是在给定相同输入的情况下返回相同的输出,等等。我喜欢通过为reducer精确提供所需的东西来减少“副作用”,尤其是因为您需要传递数据时真是太好了。

your actionCreator 您的actionCreator

export function getPokemanApiData() {
  return dispatch => {
    axios.get(APIS.getPokemanURL)
      .then((response) => {
        dispatch(getSuccess(response))
      })
      .catch((err) => {
        dispatch(getError(err))
      })
  }
}

function getSuccess(response) {
   return {
      type: TYPES.MY_POKEMAN_TYPE,
      payload: response.pokemon

   }
}

reducer 减速器

case TYPES.MY_POKEMAN_TYPE:
   return {
      ...state,
      pokemonData: action.paylod
   }

So, the way I see it - try to reduce side effects, minimize the amount of work the reducer has to do. 因此,按照我的看法-尝试减少副作用,最大程度地减少减速器要做的工作量。 Makes things easier to test too. 也使事情更容易测试。 In the example above, the reducer has exactly what it needs, returns a new state without much fuss. 在上面的示例中,Reducer完全具有所需的功能,并返回新状态而没有太多麻烦。 I've read others code in which they like to do the lifting in the reducer, not me. 我读过其他代码,他们喜欢在其中进行减速器的提升,而不是我。 I'd like to hear what others say. 我想听听别人怎么说。

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

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