简体   繁体   English

React / Redux:我该在哪里创建+放置“主”应用程序状态?

[英]React / Redux: where do I create + put the 'master' application state?

I'm getting started with pulling in Redux to a React application, but I'm having trouble understanding where exactly my 'master state design' is used. 我开始将Redux引入React应用程序,但是我在理解确切使用“主状态设计”的位置时遇到了麻烦。

For context, I'm thinking of the app state as a giant tree, and I have reducers that take care of little bits of the tree. 对于上下文,我将应用程序状态视为一棵大树,并且我使用化简器来处理树的小部分。 I've put a bit of thought into how I want the state tree to be represented, and I have a variable initialState that is basically an Immutable.js object with a bunch of child objects that contain the various parts of my app state. 我对如何表示状态树进行了一些思考,我有一个变量initialState ,基本上是一个Immutable.js对象,带有一堆子对象,这些子对象包含应用程序状态的各个部分。

I've split up my reducers to map to these various parts of my app, but I'm having trouble understanding how the giant, master state tree is created. 我已经拆分了化简器以映射到应用程序的这些不同部分,但是我在理解巨型主状态树是如何创建时遇到了麻烦。 I get that each reducer takes in the whole state tree + action and returns a new state based on the action, but I don't understand where to place the 'initial state' if the state that comes in to a reducer is undefined . 我知道每个reducer都会接受整个状态树+ action并基于该action返回一个新状态,但是如果进入reducer的状态undefined ,我不知道在何处放置“初始状态”。

In other words: is a single reducer supposed to be in charge of creating the whole state tree if it's originally undefined (and if so, where should that reducer live)? 换句话说:如果一个还原器最初未被定义(如果这样,那么该还原器应该存放在哪里),是否应该负责创建整个状态树? Or should any one reducer assign an undefined state argument to initialState variable? 还是应该有任何一个减速器为initialState变量分配一个未定义的状态参数?

If you're using combineReducers , you don't have to create the "root." 如果您使用的是combineReducers ,则不必创建“根”。 The function returned by combineReducers is itself a reducer, and it will automatically create the root of your state tree with a "branch" (property) for each of the reducers you passed in. Your reducers only need to worry about initializing their own branch with an initial state. combineReducers返回的函数本身就是一个combineReducers器,它将为combineReducers器自动创建一个带有“分支”(属性)的状态树的根。您的约简器只需要担心使用以下方法初始化自己的分支:初始状态。

If you're not using combineReducers , I think each reducer should still only worry about the part of the state tree that it acts on. 如果您不使用combineReducers ,我认为每个reducer仍然应该只担心它所作用的状态树的一部分。 Moving that into a single "master" reducer would needlessly split up related code, making your app harder to reason about. 将其移动到单个“主” reduce器中将不必要地拆分相关代码,从而使您的应用更难以推理。

As illustrated in the examples in the official guide You can use a default value for the first argument of the reducer which will be your initial state. 官方指南中的示例所示,您可以为reducer的第一个参数使用默认值,它将是您的初始状态。

function todoApp(state = initialState, action) {
  switch (action.type) {
    case SET_VISIBILITY_FILTER:
      return Object.assign({}, state, {
        visibilityFilter: action.filter
      })
    case ADD_TODO:
      return Object.assign({}, state, {
        todos: [
          ...state.todos,
          {
            text: action.text,
            completed: false
          }
        ]
      })    
    default:
      return state
  }
}

This initial state can be a default value hard coded in the client code. 此初始状态可以是在客户端代码中硬编码的默认值。 Or it can be some data bootstrapped into the HTML page by the server. 也可以是服务器将某些数据引导到HTML页面中。 For example in an EJS template rendered in server you can have: 例如,在服务器中呈现的EJS模板中,您可以具有:

<script>
window.INITIAL_STATE = <%= JSON.stringify(initialState) %>
</script>

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

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