Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting/importing utility functions between React ES6 components in React-Rails app

I want to create a utils.js that contains all of my utility functions, which are used by many of my React components.

I try to export and import a Utilities class into my React component like this:

// utils. js
export default class Utilities; 


// myComponent.es6.jsx
import someFunction from 'Utilities';

I receive errors like these:

Uncaught ReferenceError: require is not defined(anonymous function)
Uncaught ReferenceError: exports is not defined

How do I set up a separate utilities page that can then be imported into my React components?

I'm baffled -- thanks for any help you can provide!

like image 643
Adam White Avatar asked Mar 07 '26 10:03

Adam White


1 Answers

your answer could be here. Basically, I believe it's because import export and require are not yet fully compatible with Rails and all browsers. That's why in most applications they use Babel transpiler or common JS stuff. Let me know if that helps

like image 152
oreoluwa Avatar answered Mar 09 '26 22:03

oreoluwa



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!