You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native使用Axios get方法报错:undefined is not an object 如何解决?

Fixing "undefined is not an object (evaluating '_axios.axios.get')" in React Native with Axios

Hey there, let's break down how to fix this frustrating Axios error you're seeing. This issue almost always boils down to incorrect import syntax or a misconfigured Axios setup—here's what to check step by step:

1. Double-check Axios is installed properly

First, make sure you've actually added Axios to your project. Run one of these commands in your project root if you haven't already:

npm install axios
# or
yarn add axios

2. Fix your import statement (the most common fix!)

This is the #1 cause of this error. A lot of developers accidentally use named imports for Axios, but it exports a default object.

❌ Wrong way (causes the error):

import { axios } from 'axios'; // This is incorrect!

✅ Correct way:

import axios from 'axios'; // Default import is what you need

Once you fix the import, your axios.get() call should work as expected. Here's a quick example of a working get request:

import axios from 'axios';

const fetchData = async () => {
  try {
    const response = await axios.get('https://api.example.com/data');
    console.log(response.data);
  } catch (error) {
    console.error('Error fetching data:', error);
  }
};

3. If you're using a custom Axios instance

If you created a custom Axios instance (for base URLs, interceptors, etc.), make sure you're exporting and importing it correctly.

For example, in your axiosInstance.js file:

import axios from 'axios';

const instance = axios.create({
  baseURL: 'https://api.example.com',
  headers: { 'Content-Type': 'application/json' }
});

export default instance; // Export as default

Then import it in your component like this:

import axios from './axiosInstance'; // Import the custom instance

4. Clear React Native cache (if all else fails)

Sometimes React Native's metro bundler cache can hold onto old import references. Reset it with:

npx react-native start --reset-cache

That should cover the main scenarios that cause this error. Give these steps a try, and your Axios get requests should start working smoothly!

内容的提问来源于stack exchange,提问作者Abdul Hameed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:08:47