typescript react - Map重复语句不起作用问题

cuxqih21  于 2022-11-18  发布在  TypeScript
关注(0)|答案(1)|浏览(159)

版本.React:18.2.0React路由器Dom:6.4.3 @类型/React-路由器-Dom:5.3.3打印稿:4.9.3
目的是通过动态路由只显示页面所需的数据。enter image description here
尝试使用Map重复console.log中的数据,但无法正常工作。
我需要在这里修复什么?

  1. import axios from "axios"
  2. import { useState, useEffect } from "react"
  3. import { useParams } from "react-router-dom"
  4. interface ITypes {
  5. id: number;
  6. title: string;
  7. subDescription: string;
  8. description: string;
  9. }
  10. function Detail() {
  11. const params = useParams()
  12. const [data, setData] = useState([])
  13. useEffect(() => {
  14. axios.get(`http://localhost:8001/post/${params.id}`)
  15. .then((res) => setData(res.data))
  16. } , [])
  17. return (
  18. <div>
  19. {data.length >= 0 && data.map((Td:ITypes) => (
  20. <div key={Td.id}>
  21. <p>{Td.id}</p> <<<<
  22. <p>{Td.title}</p> <<< I want to repeat this part, but only white screen is rendered.
  23. </div>
  24. ))}
  25. </div>
  26. )
  27. }
  28. export default Detail
v8wbuo2f

v8wbuo2f1#

您可以尝试将本地省/市/自治区转换为:

  1. const [data, setData] = useState<ITypes[] | []>([])

相关问题