简体   繁体   English

TypeError: undefined is not a function (evaluating 'this.state.list.map')

[英]TypeError: undefined is not a function (evaluating 'this.state.list.map')

so I'm trying to push a variable onto an array called list from my state. But it is throwing off this error saying that undefined is not a function. However, when I switch push to concat, it works.所以我试图将一个变量从我的 state 推入一个名为列表的数组。但它抛出了这个错误,说未定义不是 function。但是,当我将 push 切换到 concat 时,它起作用了。 But I want to use push because it is faster.但是我想使用 push 因为它更快。 How do I fix this problem?我该如何解决这个问题?

AsyncStorage.getItem("one").then((value) => {
    if (value === "true"){
      this.setState({
        list: this.state.list.push({
          name: 'Butter',
          checkbox: 1,
        })
      })
    }
    else {
      this.setState({
        unavailable: this.state.unavailable.push(
          "butter"
        )
      })
    }
});

My list is declared like this我的列表是这样声明的

  constructor() {
    super()
    this.state = {
      unavailable: [],
      list: [] 
    }
  }

Array.push() does not return the array, it returns the array length. Array.push()不返回数组,它返回数组长度。 Thus, the list changes from an array to a number.因此,列表从数组变为数字。 It also mutates the array directly.它还直接改变数组。

As others have stated, it'd be better to do something like this:正如其他人所说,最好这样做:

this.setState({
    list: [
        ...this.state.list,
        { name: 'Butter', checkbox: 1 }
    ]
})

This way the state is not mutated directly from using push() .这样 state 就不会直接通过使用push()发生突变。

Thought I'd also explain what is happening for those interested.我想我也会为那些感兴趣的人解释发生了什么。 Instead of mutating, or changing, the ths.state.list array directly, we're copying the current array to a new array and also appending the new item at the same time.我们不是直接改变或更改ths.state.list数组,而是将当前数组复制到一个新数组并同时附加新项。

If I understand correctly you have a problem with pushing data into React state array如果我理解正确的话,你在将数据推送到 React state 数组时遇到了问题

You need to push values to array like below in React.您需要在 React 中将值推送到数组,如下所示。 You are doing incorrect way also you are never recommended to mutate any state in React.你做的不正确,也不建议你在 React 中改变任何 state。 Below is the recommended way to mutate React state array下面是改变 React state 数组的推荐方法

  this.setState(prevState => {
    list: [...prevState.list, {
      name: 'Butter',
      checkbox: 1,
    }]
  })

Like change this to就像把这个改成

   this.setState( prevState => {
    unavailable: [...prevState.unavailable, "butter"]
  })

Also keep in mind that you are not recommended to do setState in loop.另请记住,不建议您在循环中执行 setState。 When you want to modify the state value from the loop then declare a local variable and use that for assigning values from loop and finally do setState outside the loop otherwise you will get infinite warnings当你想修改循环中的 state 值时,声明一个局部变量并使用它从循环中赋值,最后在循环外执行 setState 否则你会得到无限警告

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

相关问题 反应类型错误:this.state.List.map 不是 function - React TypeError: this.state.List.map is not a function TypeError:未定义不是函数(正在评估userItems.map) - TypeError: undefined is not a function (evaluating userItems.map) TypeError:undefined不是一个函数(评估“ categories.map”) - TypeError: undefined is not a function (evaluating 'categories.map') 错误类型错误:未定义不是 object(正在评估“state.map”) - ERROR TypeError: undefined is not an object (evaluating 'state.map') 映射函数 - 未处理的 JS 异常:TypeError:undefined 不是评估映射的对象 - Map Function - Unhandled JS Exception: TypeError: undefined is not an object evaluating map TypeError: Undefined is not an object(评估'this.state.videos.map') - TypeError: Undefined is not an object (evaluating 'this.state.videos.map') TypeError:undefined不是函数(评估调度) - TypeError: undefined is not a function (evaluating dispatch) 类型错误:未定义不是对象(评估'_this3.state.bind') - TypeError: undefined is not an object (evaluating '_this3.state.bind') TypeError: undefined is not an object(评估“this.state”) - TypeError: undefined is not an object (evaluating 'this.state') TypeError: undefined is not an object(评估'_this.state.data) - TypeError: undefined is not an object (evaluating '_this.state.data)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM