简体   繁体   中英

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.

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>
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM