简体   繁体   English

如何将 function 从另一个文件导入 reactjs 组件?

[英]How to import function from another file into reactjs component?

I have a main.js file我有一个 main.js 文件

function convertToFahrenheit(param) {
    return param * 2 + 30;
}

function convertToCelsius(param) {
    return (param - 32) * 1.8;
}

I have imported it into my component but it doesn't seem to work, I have also checked the path from devtool and this file completely exists我已将它导入到我的组件中,但它似乎不起作用,我还检查了 devtool 的路径,并且该文件完全存在

import React from "react";
import TemperatureInput from "./TemperatureInput.js";
import "../assets/js/main.js";

class Caculator extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            type: "c",
            temperature: 0,
        };
    }
    handleCelsiusChange = (value) => {
        this.setState({
            temperature: value,
            type: "c",
        });
    };
    handleFahrenheitChange = (value) => {
        this.setState({
            temperature: value,
            type: "f",
        });
    }
    render() {
        let valueCelsius = this.state.type === 'c' ? this.state.temperature : convertToCelsius(this.setState.temperature);
        return (
            <div id="caculator">
                <TemperatureInput type={this.state.type} changeInput = {this.handleCelsiusChange}/>
                <TemperatureInput type={this.state.type} changeInput = {this.handleFahrenheitChange} />
            </div>
        );
    }
}

export default Caculator;

what i get is我得到的是

'convertToCelsius' is not defined  no-undef

How can I use this function in my component?如何在我的组件中使用这个 function?

Try this尝试这个

export function convertToFahrenheit(param) {
    return param * 2 + 30;
}

export function convertToCelsius(param) {
    return (param - 32) * 1.8;
}

and then in your component然后在你的组件中

import { convertToCelsius } from "../assets/js/main.js";

You need to export the functions in main.js and then use the correct syntax to import them to the component.您需要导出 main.js 中的函数,然后使用正确的语法将它们导入到组件中。 Try this:尝试这个:

export function convertToFahrenheit(param) {
    return param * 2 + 30;
}

export function convertToCelsius(param) {
    return (param - 32) * 1.8;
}

Then for importing do the following然后为导入执行以下操作

import React from "react";
import TemperatureInput from "./TemperatureInput.js";
import { convertToFahrenheit, convertToCelsius } from "../assets/js/main.js";

This site goes into more detail about it: https://www.geeksforgeeks.org/reactjs-importing-exporting/这个网站更详细地介绍了它: https://www.geeksforgeeks.org/reactjs-importing-exporting/

Use export in main.js在 main.js 中使用export

export function convertToFahrenheit(param) {
    return param * 2 + 30;
}

and named import并命名为导入

import {convertToFahrenheit} from "../assets/js/main.js";

There are multiple ways you can get function by using ES6 import and export syntax有多种方法可以通过使用 ES6 importexport语法来获取 function

In short, export allows you to export multiple expressions at a time while export default only allows one expression to be exported.简而言之, export允许您一次导出多个表达式,而export default只允许导出一个表达式。 Also, for those things exposed by export , brackets are needed when import while export default doesn't.此外,对于那些由export暴露的东西, import时需要括号,而export default不需要。

Option 1: export by named import选项 1:通过命名导入export

export function convertToFahrenheit(param) {
    return param * 2 + 30;
}

export function convertToCelsius(param) {
    return (param - 32) * 1.8;
}

//imported in your needed file
import {convertToFahrenheit, convertToCelsius} from '../assets/js/main.js'

Option 2: another way using export by name import选项 2:另一种使用按名称export的方式导入

function convertToFahrenheit(param) {
    return param * 2 + 30;
}

function convertToCelsius(param) {
    return (param - 32) * 1.8;
}

export {convertToFahrenheit, convertToCelsius}

//imported in your needed file
import {convertToFahrenheit, convertToCelsius} from '../assets/js/main.js'

Option 3: export default for one expression (function, class, object, array...)选项 3: export default (函数、class、object、数组...)

//demo for exporting 1 function

export default function convertToFahrenheit(param) {
    return param * 2 + 30;
}

//imported in your needed file, the name can be customised by you.
import myOwnFunc from '../assets/js/main.js' 

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

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