Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios is undefined in React, error: Uncaught TypeError: Cannot read property 'post' of undefined

I have installed the axios package with: npm i axios. I import it in my redux action and Im trying to axios.post(), but the axios object is undefined. It posts successfully with .fetch() I'm using "axios": "^0.19.2" & "react": "^16.13.0".

import { axios } from "axios";

// Login User
export const login = () => dispatch => {
    const requestOptions = {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ email: "[email protected]", password: "password" })
    };
    axios.post("https://freunder.uber.space/login", requestOptions)
      .then(response => response.json())
  };

Browser error

like image 812
youhatch Avatar asked Oct 25 '25 10:10

youhatch


1 Answers

Import axios as a default instead of a named export like so:

// This
import axios from 'axios';

// Instead of this
import { axios } from "axios";
like image 136
Jeremy Yip Avatar answered Oct 27 '25 00:10

Jeremy Yip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!