简体   繁体   中英

Import an include file in react component and acess the imports

I'm developing a react native app and have a component that looks like this:

import React, { Component } from 'react';
import './OtherImports';

class Products extends Component {

}

Now in another file called OtherImports.js i want to import other dependencies/modules so that i can reuse them in all my components.

In OtherImports.js

window.navigator.userAgent = 'react-native';
import io from 'socket.io-client/dist/socket.io';

Now when I include(import) this file in my component, I get an error.

How can I include this file in react components and yet be able to use them? for instance the variable io in my example

What you are trying to do (based on the comments) you are trying to import socket.io using custom file import here what you have to add to your file to make it work:

OtherImports.js

window.navigator.userAgent = 'react-native';
import io from 'socket.io-client/dist/socket.io';
export io;

and you can use export default

Note : what you could have done is to import it directly in your code where you are using it.

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