Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何返回Redux reducer的对象数组状态_Reactjs_Redux - Fatal编程技术网

Reactjs 如何返回Redux reducer的对象数组状态

Reactjs 如何返回Redux reducer的对象数组状态,reactjs,redux,Reactjs,Redux,有这样一种状态: review: { "amount": 217, "snippets": [ { "id": 0, "flag": false }, { "id": 1, "flag": false }, ... ] } 现在,如果我只想设置review.snippets[1].flag=true,那么应该在reducer中编写什么? 例如: ca

有这样一种状态:

 review: {
    "amount": 217,
    "snippets": [
      {
       "id": 0,
       "flag": false
      },
      {
       "id": 1,
       "flag": false
      },
      ...
    ]
  }
现在,如果我只想设置review.snippets[1].flag=true,那么应该在reducer中编写什么? 例如:

case SET_FLAG: return {
    ...state,
    review: {
      ...state.review,
      snippets: {
        ...state.review.snippets,
        // don't know how to write here to express array
      }
    },
};

如果您不使用CombineReducer来分隔存储区的各个部分,那么可以保持reducer大小写相似,但对snippets数组执行类似的操作

// grab your snippets
const snippets = state.review.snippets

// create a copy
const snippetsCopy = snippets.slice()

// replace the element with a new object with the same properties
// and overwrite the property you want
snippetsCopy[1] = {
  ...snippetsCopy[1],
  flag: true
}
const newSnippetsArray = Object.assign(arr, newArr)

// in your switch case return something like this:

return {
    ...state,
    review: {
      ...state.review,
      snippets: newSnippetsArray
    },
};

如果您不使用CombineReducer来分隔存储区的各个部分,那么可以保持reducer大小写相似,但对snippets数组执行类似的操作

// grab your snippets
const snippets = state.review.snippets

// create a copy
const snippetsCopy = snippets.slice()

// replace the element with a new object with the same properties
// and overwrite the property you want
snippetsCopy[1] = {
  ...snippetsCopy[1],
  flag: true
}
const newSnippetsArray = Object.assign(arr, newArr)

// in your switch case return something like this:

return {
    ...state,
    review: {
      ...state.review,
      snippets: newSnippetsArray
    },
};