Javascript state.findIndex不是findIndex的函数错误

Javascript state.findIndex不是findIndex的函数错误,javascript,reactjs,react-native,redux,react-redux,Javascript,Reactjs,React Native,Redux,React Redux,我将对象的id作为action.payload传递给减速器以修改对象 减速器: 但我得到了一个错误:state.findIndex不是一个函数。如何在posts数组中找到元素的索引?console.log操作为我提供了{type:updateNumber,payload:2},其中payload是按下的元素 更新1: 因此,这应该返回状态中带有更新编号的帖子,对吗?您的初始状态是一个对象 我想你是说 state.posts.findIndex(({ id }) => id == actio

我将对象的id作为action.payload传递给减速器以修改对象

减速器:

但我得到了一个错误:state.findIndex不是一个函数。如何在posts数组中找到元素的索引?console.log操作为我提供了{type:updateNumber,payload:2},其中payload是按下的元素

更新1:

因此,这应该返回状态中带有更新编号的帖子,对吗?

您的初始状态是一个对象

我想你是说

state.posts.findIndex(({ id }) => id == action.payload);  
或者将initialState更改为

编辑 作为编辑的后续工作, 更改后,现在您可以执行以下操作:

const number = toIncrement.number++;
因为totalIncrement将保存如下对象,例如:

{id:1, name:'post1', number:11}  
编辑2 我认为您正在改变redux中不允许的状态。 尝试更改此选项:

if (index > -1 ) {
      const toIncrement = state.posts[index];
      const number = toIncrement.posts.number++;  
为此:

if (index > -1 ) {
      const toIncrement = {...state.posts[index]};
      const number = toIncrement.posts.number + 1; // i hope this is a number and not a string!
另外,初始状态是一个对象,但减速器返回一个数组。 更改此行:

// return new array that replaces old object with incremented object at index
      return [...state.posts.slice(0, index), updatedData, ...state.posts.slice(index + 1)];  
关于这一行:

// return new array that replaces old object with incremented object at index
      return { posts: [...state.posts.slice(0, index), updatedData, ...state.posts.slice(index + 1)]};
您的初始状态是一个对象

我想你是说

state.posts.findIndex(({ id }) => id == action.payload);  
或者将initialState更改为

编辑 作为编辑的后续工作, 更改后,现在您可以执行以下操作:

const number = toIncrement.number++;
因为totalIncrement将保存如下对象,例如:

{id:1, name:'post1', number:11}  
编辑2 我认为您正在改变redux中不允许的状态。 尝试更改此选项:

if (index > -1 ) {
      const toIncrement = state.posts[index];
      const number = toIncrement.posts.number++;  
为此:

if (index > -1 ) {
      const toIncrement = {...state.posts[index]};
      const number = toIncrement.posts.number + 1; // i hope this is a number and not a string!
另外,初始状态是一个对象,但减速器返回一个数组。 更改此行:

// return new array that replaces old object with incremented object at index
      return [...state.posts.slice(0, index), updatedData, ...state.posts.slice(index + 1)];  
关于这一行:

// return new array that replaces old object with incremented object at index
      return { posts: [...state.posts.slice(0, index), updatedData, ...state.posts.slice(index + 1)]};

try state.posts.findIndexErrors:无法读取未定义状态的属性'number'。posts.findIndexErrors:无法读取未定义状态的属性'number'。posts.findIndex{id}=>id==action.payload;错误:无法读取未定义的属性“number”此错误显然是从另一行抛出的,因为您在这一行中没有使用number变量。顺便说一句,在这段代码中,您正在将状态imo:const to increment=state[index];const number=toIncrement.number++;如果你是这个意思的话,我想用这个动作增加这个数字。该错误适用于行:const number=toIncrement.number++;同样,您的状态是一个对象而不是数组。它应该是const number=toIncrement.posts.number++;state.posts.findIndex{id}=>id==action.payload;错误:无法读取未定义的属性“number”此错误显然是从另一行抛出的,因为您在这一行中没有使用number变量。顺便说一句,在这段代码中,您正在将状态imo:const to increment=state[index];const number=toIncrement.number++;如果你是这个意思的话,我想用这个动作增加这个数字。该错误适用于行:const number=toIncrement.number++;同样,您的状态是一个对象而不是数组。它应该是const number=toIncrement.posts.number++;