1

I'm able to import a vanilla.js file to my React Native project with import './vanilla';

When I try to use anything in that file, it just says Can't find variable. I know the import is succesful because if I put window.something into the vanilla file, I can call something. So I guess the question is how do I get visibility into the variables, functions, etc. that I imported from the vanilla file?

2 Answers 2

3

You should be able to do this:

import vanilla from './vanilla';

then you can use your exported functions, ie:

vanilla.someMethod();

Check out: http://www.2ality.com/2014/09/es6-modules-final.html

Sign up to request clarification or add additional context in comments.

3 Comments

how you are exporting? Could you please provider an example?
This is one of the libraries that I've been trying to work with: github.com/bitwiseshiftleft/sjcl/blob/master/sjcl.js
No problem @AtteJuvonen!
1

Thats pretty much depends on how do you export your module? You have multiple options to export your module or functions of that module.

For example:

// export data
export var color = "red";
export let name = "Nicholas";
export const magicNumber = 7;

// export function
export function sum(num1, num2) {
    return num1 + num1;
}

// export class
export class Rectangle {
    constructor(length, width) {
        this.length = length;
        this.width = width;
    }
}

// this function is private to the module
function subtract(num1, num2) {
    return num1 - num2;
}

// define a function...
function multiply(num1, num2) {
    return num1 * num2;
}

// ...and then export it later
export { multiply };

or exporting a default value such as:

function sum(num1, num2) {
    return num1 + num2;
}

export default sum;

you can read more about exporting and importing modules here: https://leanpub.com/understandinges6/read/#leanpub-auto-basic-exporting

2 Comments

I wanted to use a crypto library without modifying it. Is this possible with React Native? For example, is there a way to import this library without modifying it: github.com/bitwiseshiftleft/sjcl/blob/master/sjcl.js
It would be nice if you add some examples how to import each option of your example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.