最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to use a function from another js file in reactjs? - Stack Overflow

programmeradmin1浏览0评论

i have a file something.js which has a function:

someFunction: function(arg1, arg2){
    //code blocks
}

In my app.js file i want to call this function in the app class. I have imported the something.js file like this import { someFunction } from './something.js';. Now i am trying to use it in a function in the app class

var res = someFunction("abc", 2); 
console.log(res);`

i am getting a error Uncaught TypeError: (0 , _something.someFunction) is not a function

Some help would be appreciated.

i have a file something.js which has a function:

someFunction: function(arg1, arg2){
    //code blocks
}

In my app.js file i want to call this function in the app class. I have imported the something.js file like this import { someFunction } from './something.js';. Now i am trying to use it in a function in the app class

var res = someFunction("abc", 2); 
console.log(res);`

i am getting a error Uncaught TypeError: (0 , _something.someFunction) is not a function

Some help would be appreciated.

Share Improve this question asked Mar 27, 2017 at 12:37 ShocKwav3_ShocKwav3_ 1,7606 gold badges24 silver badges44 bronze badges 2
  • have you exported that function ? it should be like this: export someFunction: function(arg1, arg2){ //code blocks } – Mayank Shukla Commented Mar 27, 2017 at 12:38
  • Yes i did like this export default {//statements } – ShocKwav3_ Commented Mar 27, 2017 at 12:40
Add a comment  | 

4 Answers 4

Reset to default 5

You need to write it like this:

something.js file -

module.exports = {

   A: funtion(){
   },

   B: funtion(){
   }

}

Then import it like this:

import {A} from 'something';

Or use it like this:

something.js file -

export A(){
}

export B(){
}

Then import it like this:

import {A} from 'something';

Read this article: https://danmartensen.svbtle.com/build-better-apps-with-es6-modules

In order to import something, you need to export it from the other module. For example, you could export class YourComponent extends React.Component in something.js.

Then in the other file you can import { YourComponent } from './something'

You could, for example, in something.js do something like

const MyClass = {
    methodName() { return true; }
}

export { MyClass as default } // no semi-colon

Then in the other file you could

import WhateverIWant from 'something';
WhateverIWant.methodName(); // will return true

Edit: An in-depth explanation with lots of examples is available here.

You could either do: in your something.js file: module.exports = function(){}.. and in your app.js file:

const functionName = require('./path_to_your_file');

Or export somethingFunction = {} and in app.js:

import { somethingFunction } from './path_to_your_file'

Or last: export default somethingFunction = {} and in app.js:

import whateverNameYouChoose from './path_to_your_file'

Let me know if that did the trick! :)

In your something.js file, you can add export someFunction near the bottom of the file. This will then allow you to import that function using the import { someFunction } from './something.js'; you have earlier.

发布评论

评论列表(0)

  1. 暂无评论