How to include custom JS files in to React create app

后端 未结 2 635
情深已故
情深已故 2021-01-31 21:20

I had js files under public folder i want to include these js files in to react components which are under src component folder in react create application?

2条回答
  •  迷失自我
    2021-01-31 22:03

    Put them in the same folder as your react components and simply import them to you react components like so:

    // importing your js file called jslibrary.js
    // make sure that the file path is correct!
    import { function1 } from './jslibrary.js'

    You also have to make sure that you are exporting your function / variables in your jslibrary file, like so:

    export function function1() {
      //... do stuff
    }
    
    let myVar = 1234;
    export myVar;

    If you use export default on a function or a variable, you don't need to use the brackets like in the above.

    For more info, I highly recommend the documentation on import and export.

提交回复
热议问题