简体   繁体   English

反应 Js:.map 不是 function

[英]React Js : .map is not a function

I have json array as below -我有 json 阵列如下 -

[{"sup_Id":6,"sup_ShortCode":"A"},{"sup_Id":7,"sup_ShortCode":"B"},{"sup_Id":8,"sup_ShortCode":"C"},{"sup_Id":1000,"sup_ShortCode":"D"}]

React component reading this array as -反应组件读取此数组为 -

import React,{useEffect,useState} from 'react'
import axios from 'axios';
function AllSuppliers() {
    const [suppliers, setstate] = useState([])
    useEffect(() => {
        // GET request using axios inside useEffect React hook
        axios.get('http://localhost:62815/api/values/GetAllSuppliers')
            .then(x => setstate({suppliers:x.data}))
            .catch(error => {
                alert(error);
                
            });;
    }, []);
    return (
        <>
            <table style={{width: '50%'}}>
            <thead>
                <tr>
                <th>
                    Supplier Id
                </th>
                <th>
                    Supplier Name
                </th>
                
                </tr>
                </thead>
                {
                   
                    suppliers.map((supplier)=>supplier)
                }
            </table>
        </>
    )
}

export default AllSuppliers

I am getting -我正进入(状态 -

TypeError: suppliers.map is not a function类型错误:供应商。map 不是 function

error错误

I also tried with - suppliers[0].map since its an array.我还尝试了suppliers[0].map ,因为它是一个数组。 But this also did not worked.但这也没有奏效。

Use this way as you are using Hooks在使用 Hooks 时使用这种方式

setstate(x.data)

instead of this way which uses in class component而不是在 class 组件中使用的这种方式

setstate({suppliers:x.data})

Update the useState initialization as:将 useState 初始化更新为:

const [suppliers, setSuppliers] = useState([])

(It is more meaningful) (更有意义)

Also, Update the useEffect as:此外,将 useEffect 更新为:

useEffect(() => {
        axios.get('http://localhost:62815/api/values/GetAllSuppliers')
            .then(x => setSuppliers(x.data))
            .catch(error => {
                alert(error);
            });;
    }, []);

You need not set the state in an object when using React Hooks unlike normal class state.当使用 React Hooks 时,您无需在 object 中设置 state,这与普通 class Z9ED39E2EA9314256B6EZ9857A 不同。

You are setting your suppliers state to be an object in this line:您在此行中将您的供应商 state 设置为 object:

.then(x => setstate({suppliers:x.data}))

Instead, let the setstate handle the change of suppliers and only pass the variable like this:相反,让 setstate 处理供应商的变化,只传递这样的变量:

.then(x => setstate(x.data))

assuming x.data is an array.假设 x.data 是一个数组。

You can also change the following line suppliers.map((supplier)=>supplier) to (suppliers || []).map((supplier)=>supplier) this way in case x.data or suppliers get changed to null\undefined your app will not crash.如果(suppliers || []).map((supplier)=>supplier)suppliers.map((supplier)=>supplier)商更改为 null\ undefined 你的应用程序不会崩溃。

Also as mentioned above the usage of setstate is not recommended.同样如上所述,不建议使用 setstate。 I'd advise you read about this topic here: https://reactjs.org/docs/hooks-state.html我建议您在此处阅读有关此主题的信息: https://reactjs.org/docs/hooks-state.html

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

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