【问题标题】:React - How to export an array from a JS file by making axios call and use it in another JS file?React - 如何通过 axios 调用从 JS 文件中导出数组并在另一个 JS 文件中使用它?
【发布时间】:2021-11-18 11:24:03
【问题描述】:

我基本上有两个名为 a.js 和 b.js 的文件。

b.js 正在通过对 API 进行 axios 调用来获取一些数据来构建一个数组,并根据该数据相应地填充该数组。我必须让 b.js 返回该数组,以便我可以在其中使用它一个.js

需要注意的是,在 b.js 中进行 axios 调用时,我必须提供用户的身份验证 令牌作为参数。

在 b.js 中

const b = () => {

//Getting the user authentication token
const [token, setToken] = userSessionStorage('details' null)
  
  var myarray = []
  
  useEffect(() => {
  
    //Making axios call to get data
    //depending on data building the array
    //As parameter to the axios call I am sending the user's authentication 
    
    //axios call
    //condition was what was get from the call
    
    if(condition){
        myarray.push(data1)
    }
    else{
        myarray.push(data2)
    }
    
    
    return myarray
    
  });
  
  
}

export default b

在 a.js 中

import b  from "./b.js";

console.log(b) <-------------- GETTING UNDEFINED

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript reactjs function import react-router


    【解决方案1】:

    检查 a.js 和 b.js 是否在同一级别。休息一切看起来都很好。

    【讨论】:

    • 您好,感谢您的回复,但他们确实在同一级别
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2021-04-26
    • 2016-07-03
    • 2023-02-01
    • 1970-01-01
    • 2017-05-02
    相关资源
    最近更新 更多