10

I would like to create a file (function.js) for a function that does this:

let i = 0;
    if (this === that) {
        i = 0;
    } else {
       i = 1;
    }

I would then like to add it to (this.js)

import function from "./function";
class Example extends Component {
    state = {
        test
    };
    render() {
        function()
    return (
        <div>
            <h1>{this.state.test.sample[i].name}</h1>
        </div>
user10916917
  • 113
  • 1
  • 1
  • 6
  • export the function.js first. then you can import it. –  Jan 15 '19 at 12:53
  • Hi, try exporting function in functions file, and import it in your other file. so the exporting statement would be like export default functionName; – Asim Khan Jan 15 '19 at 12:54

6 Answers6

20

You can do something like:

function.js

const doSomething = function() {
let i = 0;
    if (this === that) {
        i = 0;
    } else {
       i = 1;
    }

}

export default doSomething;

App.js (for example):

import doSomething from "./function";

class Example extends Component {
    state = {
        test
    };
    render() {
        doSomething()
    return (
        <div>
            <h1>{this.state.test.sample[i].name}</h1>
        </div>
     )
Tarreq
  • 1,282
  • 9
  • 17
  • You are not assigning value of i to anything in your main component. for example you can return i from your function, and then after importing function into your file, you can do: let result = doSomething(); then you have the value of i, to assign it to whatever you want. – Tarreq Jan 15 '19 at 14:10
  • 1
    Thank you so much, holy crap, that makes so much sense! – user10916917 Jan 15 '19 at 14:38
13

there are multiple ways to do it.

1st

If you are going to create multiple functions in that file

export const one = () => {
let i = 0;
    if (this === that) {
        i = 0;
    } else {
       i = 1;
    }
}

export const two = () => {
let i = 0;
    if (this === that) {
        i = 0;
    } else {
       i = 1;
    }
}

And then import and use it

import { 
one, 
two 
} from "./functions"

2nd

You can use export defualt

export default = function() {
  let i = 0;
  if (this === that) {
    i = 0;
  } else {
    i = 1;
  }
}

and then simply use it by doing this

import function from "./function";
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
2

The function keyword is a reserved identifier.

On the browser you need some kind of bundler tool which will allow to import from a js module. On the server you can just require(path/to/file). I suggest you look at create-react-app for a fully functionnal react setup. The basic setup contains example about the JS module system (see docs below).

In return your file needs to export the symbols you want to use.

In a directory with a.js and b.js where b wants to import a symbol from a

// file a.js
export function myFunction () {}


// file b.js
import { myFunction } from "./a";

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://github.com/facebook/create-react-app

adz5A
  • 2,012
  • 9
  • 10
1

function.js has below code

const funcName = function() {
  let i = 0;
  if (this === that) {
    i = 0;
  } else {
    i = 1;
  }
}

export default funcName;

And you can use it in this.js as below -

import funcName from "./function"; // or wherever the file is saved

class Example extends Component {
    state = {
        test
    };
    render() {
        funcName();
      return (
        <div>
            <h1>{this.state.test.sample[i].name}</h1>
        </div>
       );
     }
alien_jedi
  • 306
  • 3
  • 11
Rishikesh Dhokare
  • 3,559
  • 23
  • 34
1

Export your function like this in function.js

export function funcName() {
   //function stuff
   let i = 1;
   return i;
}

the import would be

import { funcName } from './function';

console.log(`value of i is ${funcName()}`);
Tobias
  • 229
  • 1
  • 2
  • 13
0

Try this

another-file.js

export function sum(a, b) {
  return a + b;
}

export function ShowText() {
  return '<h1>This is a sample Page</h1>';
}

App.js

import {sum, ShowText} from './another-file';

export default function App() {
  return (
    <div>
      <h2>5 + 5 = {sum(5, 5)}</h2>
      <hr />

      <h2>Text From another file: {ShowText()}</h2>
    </div>
  );
}
Merrin K
  • 1,602
  • 1
  • 16
  • 27