简体   繁体   English

使用 useState 反应钩子后返回的 state 变量显示。map 不是 function

[英]The state variable returned after using useState react hook shows .map is not a function

This code gives me error when I try to map the state variable returned after calling useEffect hook.当我尝试 map 调用 useEffect 挂钩后返回的 state 变量时,此代码给了我错误。 On console, when I print the myAppointment it shows two values one is empty ([]) and other (Array[25]) I want to extract the petname values from this array.在控制台上,当我打印 myAppointment 时,它显示两个值,一个是空的 ([]),另一个是 (Array[25]) 我想从这个数组中提取 petname 值。 But getting error "map is not a function" Please look into it and help me!但是出现错误“地图不是函数”请查看并帮助我!

function App() {
  const [myAppointment, setmyAppointment] = useState([])
  useEffect(() => {
      fetch('./data.json')
      .then(response=> response.json())
      .then(result=>{
        const apts=result.map(item=>{
          return item
        })
        setmyAppointment(state=>({...state,
          myAppointment:apts
        }))
      })**strong text**
  },[])

  console.log(myAppointment)
  const listItem = myAppointment.map((item)=>{
    return(
      <div>{item.petName}</div>
    )
  })

  return (
    <main className="page bg-white" id="petratings">
    <div className="container">
      <div className="row">
        <div className="col-md-12 bg-white">
          <div className="container">
            {/* {listItem} */}
            <div><AddAppointment /></div>
            <div><SearchAppointment /></div>
            <div><ListAppointment /></div>
          </div>
        </div>
      </div>
    </div>
  </main>
  );
}

You have you state as an array.你有你 state 作为一个数组。 However when you try to update it you convert it into an object with key myAppointment which is the cause of your error.但是,当您尝试更新它时,您将其转换为 object 密钥myAppointment是您错误的原因。

You must update your state like您必须像这样更新您的 state

  setmyAppointment(state=> [...state, ...apts]);

However since the state is initially empty and you only update it once in useEffect on initialLoad, you could also update it like然而,由于 state 最初是空的,并且您只在 initialLoad 的 useEffect 中更新它一次,您也可以像这样更新它

 setmyAppointment(apts);

The problem is from the way you're setting your state after the data is fetched.问题在于您在获取数据后设置 state 的方式。

You're setting the state as an object by instead of an array.您将 state 设置为 object 而不是数组。

Let's Take a look at the line where you set the state.让我们看一下设置 state 的行。

setmyAppointment(state=>({...state,
          myAppointment:apts
        }))

Using the spread operator with curly braces tells JavaScript compiler to spread an object, and obviously that's not what you want.使用带有花括号的展开运算符告诉 JavaScript 编译器展开 object,显然这不是您想要的。

What you ought to do is instead of curly braces, use square brackets.你应该做的不是花括号,而是使用方括号。 That will tell the compiler you want to spread an array.这将告诉编译器您想要传播一个数组。

So it should be something like this.所以它应该是这样的。


setmyAppointment(state=>([...state
        ]))

And if I were you, I will do this a little bit different.如果我是你,我会做这件事有点不同。 I will set the state like this我将像这样设置 state

setmyAppointment(apt)

I hope this helps我希望这有帮助

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

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