简体   繁体   English

在没有 ES6 的 React 中导出和导入类

[英]Export and import classes in React without ES6

I'm working on a react project but don't want to use ES6 , because I don't really understand how it works.我正在做一个 react 项目,但不想使用ES6 ,因为我真的不明白它是如何工作的。 I want to export my class, ShoppingList, given below:我想导出我的类 ShoppingList,如下所示:

class ShoppingList extends React.Component {
    render() {
        return (
            <div className="shopping-list">
                <h1>Shopping List for {this.props.name}</h1>
                <ul>
                    <li>Instagram</li>
                    <li>WhatsApp</li>
                    <li>Oculus</li>
                </ul>
            </div>
        );
    }
}

I want to import this into my index.js:我想将它导入到我的 index.js 中:

const rootElement = document.getElementById('root')
const ShoppingList = require('./Components/ShoppingList')

function App() {
    return (
        <div>
            <ShoppingList name="Abhinav" />
        </div>
    )
}

ReactDOM.render(<App />, rootElement)

But the require() function doesn't work.但是require()函数不起作用。 If anyone could tell me how to easily import ta function or how I could add ES6 to this.如果有人能告诉我如何轻松导入 ta 函数或如何将 ES6 添加到其中。 The image of the directory is given below:该目录的图像如下:

在此处输入图片说明

dont want to use ES6, because I dont really understand how it works.不想使用 ES6,因为我真的不明白它是如何工作的。

For this you have to take a look at the standards define for the language.为此,您必须查看为语言定义的标准。 You can start here . 你可以从这里开始 Most of the browswers today supports ES6+ features like class, fat arrow syntax, let, const etc.今天的大多数浏览器都支持 ES6+ 特性,如类、粗箭头语法、let、const 等。

For full support you have to set up any transpiler which is used to transpile latest standard code to working supported code.要获得全面支持,您必须设置任何用于将最新标准代码转换为工作支持代码的转译器。 What this means is it will transpile the code to supported code for the browsers which does not understand the latest syntax.这意味着它将代码转换为不理解最新语法的浏览器支持的代码。

For this you can use several tools like:为此,您可以使用多种工具,例如:

  • Webpack网络包
  • Babel通天塔
  • Typescript打字稿

etc. Typescript is a language which is a superset of javascript.等等。Typescript 是一种语言,它是 javascript 的超集。 It has all the latest features of javascript which is not yet released or supported.它具有尚未发布或支持的 javascript 的所有最新功能。

If anyone could tell me how to easily import a function or how I could add ES6 to this.如果有人能告诉我如何轻松导入函数或如何将 ES6 添加到其中。

See, if you want to import any function or class then it must be exported first.看,如果你想导入任何函数或类,那么它必须先导出。 So, if we take your code example then you have to export it like:因此,如果我们采用您的代码示例,那么您必须将其导出为:

export class ShoppingList extends React.Component {
     // class code
}

or或者

class ShoppingList extends React.Component {}
export { ShoppingList }; // normal export
export default ShoppingList; // default export don't need {}

In the above code you can see two types of export first one is inline export and you can default export it too like export default class ShoppingList ... .在上面的代码中,您可以看到两种类型的导出,第一种是内联导出,您也可以默认导出它,例如export default class ShoppingList ...

In the second example you can see two types of exports:在第二个示例中,您可以看到两种类型的导出:

  • Normal export will be exported within {} and imported in {} too.正常的出口将在出口{}和进口在{}过。
  • Default export will be exported without braces and imported without braces too.默认导出将不带大括号导出,也将不带大括号导入。

Now you can import it like:现在你可以像这样导入它:

import { ShoppingList } from './path/of/file'; // normal import
import ShoppingList from './path/of/file'; // default exported import    
import SList from './path/of/file'; // name it anything but works only for default exports

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

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