简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM