简体   繁体   中英

Exporting 2 different functions in React

import React from 'react'

function subValues() {
   let num1, num2, res;
   num1 = Number(document.sub.txtnum1.value)
   num2 = Number(document.sub.txtnum2.value)
   res = num1 - num2;

   document.sub.txtres.value=res

}

function Sub() {
   return (
       <div>
           <form>
               First number:<input type="number" className="txtNum1"></input>
               Second number:<input type="number" className="txtNum2"></input>
               Result: <input className="textres"></input>
               <input type="button" value="Calculate" onClick="subValues()" ></input>

           </form>
       </div>
   )
}

export {
   Sub,
   subValues
}

I am trying to export both of these functions. I am learning how to use React Router right now and trying to create a very basic calculator with a Single Page Application. It does not like it when I try to put the subValues function inside of the Sub function. I have tried different ways to export that was shown in exporting multiple modules in react.js this thread, but it did not like it when I tried those ways at all. What is the correct way to get both of these to export and be usable with my App component? Thank you so much.

ps I do know that the code that I have written up right here will probably not work and is probably wildly wrong. I just typed all of this up and am trying to get to be able to test my code but can't until I can use both functions

You can use:

A.js

export function myFunction() {}

B.js

import { myFunction } from './A'

You can export on different lines and the import like this

 // Sub.js import React from 'react' export function subValues() { // content } export function Sub() { // content } // and where you want to import this, App.js import { Sub, subValues } form './Sub';

You export like this:

export const subValues = () => {}
export const Sub= () => (<div> ... </div>);

And to import you can do this:

import { subValues , Sub } from './youClass.js';

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