1

I'm using the latest create-ract-appversion for my project and I have a custom.js file with a CustomFunction function in it. I need is to access to the CustomFunctionfunction from one of my components.

Which is the best way to achieve this? Importing the file into index.html like below, I have 'CustomFunction' is not defined no-undef' error:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">

    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">

    <!-- jQuery import -->
    <script
            src="https://code.jquery.com/jquery-3.2.1.min.js"
            integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
            crossorigin="anonymous"></script>

    <!-- Custom Style and JS import -->
    <link rel="stylesheet" href="./CustomComponent/custom.css">
    <script src="./CustomComponent/custom.js"></script>

This is the project structure:

10 ├── public
 11 │   ├── customComponent
 12 │   │   ├── custom.js
 13 │   │   └── custom.css
 14 │   ├── favicon.ico
 15 │   ├── index.html
 16 │   └── manifest.json

1 Answer 1

4

What you need to do is export the function form you custom.js file like

const CustomFunction = () => {

}

export {CustomFunction};

Now you can import it in any of the component like

import {CustomFunction} from './path/to/custom.js';

If you have multiple of them in component.js, which you want to use in other components then you can export them like

export {
    CustomFunction,
    CustomVariable,
    CustomVar2,
    CustomFunction2
}

And import like

import {CustomFunction, CustomVariable, CustomVar2, CustomFunction2} from './path/to/custom.js';
Sign up to request clarification or add additional context in comments.

6 Comments

Do I need to export each single variable and function form my Custom file? There are a lot of those... Is it the right way to go?
Please don't mix CommonJS syntax (module.exports) with ES6 syntax (import). Either use one or the other, not both.
Ok @DanAbramov, I will update the answer as soon as I reach home
Also no, you don't need to export every variable. You need to export only what you will use (the component in this case). See how src/App.js exports a component and src/index.js imports it in the newly created project. The User Guide also includes instructions on importing files: github.com/facebookincubator/create-react-app/blob/master/…
@DanAbramov, Yes you only need to export variable that you later on will be using, I though it was evident and did not add it in the answer explicitly
|

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.