takesuma
takesuma

Reputation: 151

Export a function: can't find variable

I try to create a function and export it to be called in differents components but i don't know very well how it work and an error occur when I do this:

function code:

export function myFunction( param ) {
   //my code...
   var ret = param
   return ret;
};

Call in the component:

import {components} from 'react'
import {myFunction} from './class.js';
import {Alert} from 'react-native';

class myClass extends Component{
  componentDidMount() {
    var val = myFunction('the param');
    Alert.alert(val);
  }
}

And occur this error:

Can't find variable: myFunction

Thanks for help.

Upvotes: 1

Views: 1524

Answers (2)

Asif vora
Asif vora

Reputation: 3359

export default function myFunction( param ) { //my code... var ret = param return ret; };

Try it and let me know its helpful.

Upvotes: 1

Roshan Gautam
Roshan Gautam

Reputation: 510

you are doing it wrong. Here is the right way to do it. Use const instead of function. And also, you can always use arrow function as below:

export const myFunction = ( param ) => {
  //my code...
  var ret = param
  return ret;
};

Now you can import myFunction as you did above. Hope it helps.

Upvotes: 0

Related Questions