Changhoon Lee Changhoon Lee - 6 months ago 64
Javascript Question

How to import&use javascript functions in jsx?

I want to use connect() javascript function in App.jsx.
I'm using meteor framework.

App.jsx

import React from 'react';
/* [I THINK THAT SOME IMPORT CODES SHOULD BE HERE..] */

export default class App extends React.Component {
render() {
return (
<div>
<button
onClick={ /* [I WANT TO USE connect() FUNCTION HERE] */ }
> Connect </button>
</div>
);
}
}


connect.js

function connect() {
console.log('connected');
}


Thank you.

Answer

You can export and import this function.

connect.js

export function connect() {
  console.log('connected');
}

App.jsx

import React from 'react';
import {connect} from '/path/to/connect.js'

export default class App extends React.Component {
  render() {
    return (
      <div>
        <button 
          onClick={connect}
        > Connect </button>
      </div>
    );
  }
}