Hamxa
Hamxa

Reputation: 19

Error in react while importing the function

I am facing the compile-time error on this

import createProfile from "../../actions/profileActions";

error is following Failed to compile.

[1]
[1] ./src/components/create-profile/CreateProfile.js
[1] 424:57-70 "export 'default' (imported as 'createProfile') was not found in '../../actions/profileActions

but the function being imported are available in profileActions.js

export const createProfile = (profileData, history) => dispatch => {
  axios
    .post("/api/profile", profileData)
    .then(res => history.push("/dashboard"))
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    );
};

Upvotes: 1

Views: 681

Answers (3)

import createProfile as import {createProfile} from "../../actions/profileActions"; other wise export it as

const createProfile = (profileData, history) => dispatch => {
  axios
    .post("/api/profile", profileData)
    .then(res => history.push("/dashboard"))
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    );
};
export default createProfile;

Upvotes: 1

Thakur Karthik
Thakur Karthik

Reputation: 3518

Yes the mistakes done by first time learners of React.

To use a function defined in other file you

1.Export the function from the file it is defined. 2.Import the function in the new file.

It can be done in two ways.

Syntax 1:

export function someFunction(){}
export const constant1="ABC"

Use the above syntax when then are multiple functions/constants you want to export.

In this case if you want to import then follow this syntax.

import {function1,constant1,...} from <file-name>

Syntax 2:

Default exports works only one per file.i.e,you can't export two functions/constants the default way

export default function1=()=>{}//okay!!

export default function1=()=>{};
export default const someconstant;//ERROR !!

You can import now like this.

import function1 from <file-name>

Upvotes: 3

Anis Smail
Anis Smail

Reputation: 779

You need either to import createProfile a single import

import {createProfile} from "../../actions/profileActions";

or export it as the default export

export default createProfile

Upvotes: 2

Related Questions