试图理解:从"React"导入React,{component}; [英] trying to understand: import React, {component} from 'React';
问题描述
我正在研究如何使React.js与后端一起工作.我已经看到了这种语法:
I am working on how to have React.js work with a backend. I have seen this syntax:
从'React'导入React,{component};
import React, {component} from 'React';
为什么在这里列出了{component}?在我看来,您当然希望使用组件.所以我不知道为什么{component}在那里.
Why is {component} listed there? In my mind, you would of course want to use components. So I don't know why {component} is there.
我认为从"React"导入React就足够了……意思是把所有的React都给我".但是显然我在这里缺少某种概念.
I would think import React from 'React' would be enough... meaning "give me all of React". But obviously I am missing some sort of concept here.
谢谢.
推荐答案
React
是 react
包的默认导出,而 Component
是该程序包中的命名导出.你可以做
React
is the default export from the react
package, whereas Component
is a named export in that package. You can either do
import React from 'react';
class MyClass extends React.Component { ... }
或
import React, { Component } from 'react';
class MyClass extends Component { ... }
在此处中了解有关JavaScript导出的更多信息.>
Read more about JavaScript exports here
编辑:实际上,React使用一种不同的模式,称为在默认导出中公开API,它具有与命名导出相同的导入约定-您可以查看其源
Actually, React uses a different pattern called exposing an API on the default export, which has the same import convention as named exports - you can look at their source here
这篇关于试图理解:从"React"导入React,{component};的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!