简体   繁体   English

在 React 中导出 2 个不同的函数

[英]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.我现在正在学习如何使用 React Router,并尝试使用单页应用程序创建一个非常基本的计算器。 It does not like it when I try to put the subValues function inside of the Sub function.当我尝试将 subValues 函数放在 Sub 函数中时,它不喜欢它。 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.我尝试了不同的导出方式, 在 react.js这个线程中导出多个模块中显示了这一点,但是当我尝试这些方式时它根本不喜欢它。 What is the correct way to get both of these to export and be usable with my App component?将这两个导出并与我的 App 组件一起使用的正确方法是什么? 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. ps 我确实知道我在这里写的代码可能不起作用,而且可能是非常错误的。 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 js

export function myFunction() {}

B.js 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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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