【问题标题】:React (Map , Axios , useState) add usestae new value using map with axiosReact (Map , Axios , useState) 使用带有 axios 的 map 添加 usestae 新值
【发布时间】:2021-12-04 19:50:55
【问题描述】:

这是一个使用状态,所以我想使用地图添加更多客户

 useEffect(() => {   
     getApiDAta()
     async function getApiDAta() {
         try {
           const response = await axios.get('http://localhost:5000/custumer/')
           .then(res => {setcustName(res.data)
           custName.map((items) => { 
             setSuggestions([items.firstname])
             console.log(suggestions);
             })

         })
           
         } catch (error) {
           console.error(error);
         }
       }
 },[]);
const [custName,setcustName] = useState([])
   const [suggestions,setSuggestions] = useState(
        custName.map((items) => {
          return   [`${items.name}`];
       })
    );

我想在建议中添加多个项目,所以请帮助我

【问题讨论】:

    标签: reactjs react-hooks


    【解决方案1】:

    您正在尝试映射一个空数组。填充后尝试映射它:

    custName.map((items) => {
          return [`${items.name}`];
       })
    

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 1970-01-01
      • 2021-12-08
      • 2021-12-16
      • 2021-02-14
      • 1970-01-01
      • 2018-06-30
      • 2020-02-02
      • 1970-01-01
      相关资源
      最近更新 更多