简体   繁体   English

reactjs 无法使用 map 方法在我的 jsx 中显示数据

[英]reactjs can't show data in my jsx using map method

Hi I am using redux but can't show data in my jsx using map method.您好,我正在使用 redux,但无法使用 map 方法在我的 jsx 中显示数据。 getting this error Cannot read properties of undefined (reading 'data') see also screenshot2 where I can see my data is receiving from api. If my data is receiving then why getting this error?收到此错误Cannot read properties of undefined (reading 'data')另见 screenshot2 我可以看到我的数据是从 api 接收的。如果我的数据正在接收,那么为什么会出现此错误?

const PostAds = () => {
  const dispatch = useDispatch()
  const [category,setCategory] = useState([])
  useEffect(()=>{
    const fetchcategory = async ()=>{
    const data = await axios.get("http://127.0.0.1:8000/ads_category/")
    const r = dispatch(ads_category(data)) 
    setCategory(r)
    } 
    fetchcategory()

  },[])

  useEffect(() => {
   
     console.log("redux_data",category)
  }, [category]);

  
    return (
      <>
       {category.payload.data.map((data)=>{
        return(data.main_category)
       })}
     </>
     
  )
}

export default PostAds

seceenshot2二次截图

在此处输入图像描述

screnshot1截图1 在此处输入图像描述

Be consistent with your types.与您的类型保持一致。

According to the data in the screen shot, you expect category to be an object with a property called payload .根据屏幕截图中的数据,您希望category为 object 并具有名为payload的属性。 But you're initially setting category to an empty array:但是您最初将category设置为空数组:

const [category,setCategory] = useState([])

So should it be an object or an array?那么它应该是一个 object 还是一个数组? An array will never have a property called payload .数组永远不会有一个名为payload的属性。

First, don't set it to an array if you don't want it to be an array.首先,如果您不希望它成为数组,请不要将其设置为数组。 If you give it no initial value at all, it will be undefined :如果你根本不给它初始值,它将是undefined

const [category,setCategory] = useState()

Then in the JSX, use optional chaining when referencing it so that if it's undefined it just outputs nothing:然后在 JSX 中,在引用它时使用可选链接,这样如果它undefined ,它就不会输出任何内容:

<>
  {category?.payload.data.map((data)=>{
    return(data.main_category)
  })}
</>

If there are cases where the payload property might not be defined, or any other property, you could use the same approach.如果在某些情况下可能未定义payload属性或任何其他属性,您可以使用相同的方法。

Firstly, category is a simple state variable in this case, and doesn't have to do anything with redux. You must use the state stored in redux, not the component itself.首先,在这种情况下, category是一个简单的 state 变量,不需要对 redux 做任何事情。您必须使用存储在 redux 中的 state,而不是组件本身。 For that matter, dispatch(ads_category(data)) should suffice.就此而言, dispatch(ads_category(data))就足够了。

Secondly, your useEffect is called AFTER the first render, which means you are only fetching data into category after the component has already tried to render once.其次,您的useEffect在第一次渲染后被调用,这意味着您仅在组件已经尝试渲染一次后才将数据提取到category中。 So category.payload.data.map will obviously error out since category doesn't have the value you want it to have.所以category.payload.data.map显然会出错,因为category没有你想要的值。

Since this is a basic problem, try to understand why you're getting this error so it doesn't repeat in the future.由于这是一个基本问题,请尝试了解您收到此错误的原因,以免将来重复出现。

For an immediate solution, try going with this:要立即解决,请尝试使用以下方法:

{category?.payload?.data?.map((data)=>{
    return(data.main_category)
})}

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

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