React-如何导出纯无状态组件 [英] React - How to export a pure stateless component
本文介绍了React-如何导出纯无状态组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何导出无状态的纯哑组件?
How can I export a stateless pure dumb component?
如果我使用类,这行得通:
If I use class this works:
import React, { Component } from 'react';
export default class Header extends Component {
render(){
return <pre>Header</pre>
}
}
但是,如果我使用纯函数,则无法正常工作.
However if I use a pure function I cannot get it to work.
import React, { Component } from 'react';
export default const Header = () => {
return <pre>Header</pre>
}
我缺少基本的东西吗?
推荐答案
ES6不允许export default const
.您必须先声明该常量,然后将其导出:
ES6 doesn't allow export default const
. You must declare the constant first then export it:
const Header = () => {
return <pre>Header</pre>
};
export default Header;
存在此约束是为了避免写被禁止的export default a, b, c;
:默认情况下只能导出一个变量
This constraint exists to avoid writting export default a, b, c;
that is forbidden: only one variable can be exported as default
这篇关于React-如何导出纯无状态组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文