Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imported function is undefined in React

In file1.js:

const doSomething = () => {
  console.log('yay');
};

export { doSomething }; 
//simplified, not using export default due to multiple exports

In file2.jsx:

import { doSomething } from './file1.js';

doSomething(); //undefined 

Been trying to solve this for a while now. Trying to import this method from one class and add it as an onClick for a component, but it's always undefined. Help would be appreciated

edit1: fixed import in simplified code

like image 692
GIyds Avatar asked Nov 27 '22 00:11

GIyds


1 Answers

You can try this. import file1.js in file2.js like this

import * as demo from './file1.js';

and call your function like this

demo.doSomething()

like image 172
Nagesh Fultambkar Avatar answered Nov 29 '22 05:11

Nagesh Fultambkar