1

I am currently studying ReactJS. I'm coding in JSX style, but I have a question. How do I use the regularly-formatted JavaScript library I used?

E.g

myScript.js

(function () {
  var myTemp = "myTemp";

  function myFunction() {
    console.log("myFunction()")
  }
  function myFunction1() {
    console.log("myFunction()")
  }
  function myFunction2() {
    console.log("myFunction()")
  }
  function myFunction3() {
    console.log("myFunction()")
  }
}

When you have a JavaScript library of the above source, What methods can you use in JSX or ReactJS? Do I have to convert this to JSX and use it?


process.js is

import myScript from '~~~~/myScript'

export function proc() {
  myScript.myScript.myFunction();
}

sample.js is

import process from '~~~~/process'

export function sample() {
  process.proc();
}

Sample.js

import React, {Component} from 'react';
import * as sample from '~~~.sample';

class mySample extends Compoonent {

 click = () => {
    sample.sample()
  }

 render() {
    return (
      <button onClick={this.click}> button </button>
    )
 }
}
export default Sample;

i don't know how to import myScript.js ( normal format javascript like Library ) and call MyScript.js functions...

7
  • 1
    Where do you want to put this code? Commented Jan 23, 2019 at 4:49
  • that code in myScript.js and i want to use all of this script function in sample.js. Commented Jan 23, 2019 at 5:19
  • Can you show me your sample.js I'll try to show you how to do it Commented Jan 23, 2019 at 5:21
  • @BonAndreOpina i added sample code Commented Jan 23, 2019 at 7:32
  • As you see in myScript you are not exporting something... Is that the full code? If that is your final code I will post an answer Commented Jan 23, 2019 at 7:43

3 Answers 3

3

Even reactjs itself is just a javascript library. It is essentially still javascript.

When you write in 'JSX', for example

const ele = <h1 className="foo">bar</h1>

It still gets compiled into regular JS function, something like this:

const ele = React.createElement('h1', {className: 'foo'}, 'bar')

The point here is, you are not supposed to learn React like you're learning something different than regular Javascript.

Get down to the fundamental of Javascript first, especially all the ES6 syntax like arrow function () => {}, spread syntax ..., class, destructuring.

It wouldn't take you long to learn those, and once you got down the fundamentals, you would understand that you can just use the block of code you posted here in React as it is.

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

Comments

2

You cannot convert this to JSX.

What you can do is...

const export OuterTemp = () => () => {
var myTemp = "myTemp";
console.log("myFunction()") 
};

import this function as :

import {outerTeamp} from './fileName.js';

9 Comments

what does this means () => () =>
function inside function. You can call this outerTeamp()()
What is the point using that. Does it have any advantages over just using () => {} and call with outerTeamp()
This is ES6 features. easy to write.
I am aware of ES6 arrow functions, My question is why do we need function inside a function ? Isn't a simple function enough? like OuterTemp = () => { }
|
1

As @Anil Kumar mentioned you can wrap the entire code in an external library to const variable and export it. You need not make any changes to inner functions since you are using previously written code.

const export OuterTemp = () => {
  var myTemp = "myTemp";
  function myFunction() {
    console.log("myFunction()")
  }
}

and you can import them and call any functions within it

import OuterTemp from '~~~/OuterTemp'

It will work as expected

Edit

As per my understanding myFunction will be considered as private function and cannot be accessed outside. even in traditional JavaScript method you can't access it out side the function wrapper.

If your intention is to create a external library that has n number of functions and that has to be accessed in another file you have to do this in below way

  export var myTemp = "myTemp";

  export var myVar = 5;
  export function myFunction(){
    console.log("myFunction()");
  }
  export function myFunction1() {
    console.log("myFunction()")
  }
  export myFunction2() {
    console.log("myFunction()")
  }
  export myFunction3() {
    console.log("myFunction()")
  }

and you can Use that like

import * as outerTemp from './myScript';

outerTemp.myFunction();

3 Comments

if the source(js file) is like... (function () { function test() { console.log } }) then... what can i do?
and how can i call myFunction() on other js
Were it my call this would be the accepted answer.

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.