【问题标题】:I want export more than one 'Pure component'我想导出多个“纯组件”
【发布时间】:2018-05-30 00:27:39
【问题描述】:

我正在使用 React Native 制作应用程序。 我注意到奇怪的事情。

除非我将最后一句更改为

,否则这段代码会出错

export default MyButton3;

我想为每个文件导出多个纯组件。如果我不使用纯组件声明,我可以这样做。

但为什么我不能用纯组件做到这一点?

谢谢。

const MyButton3 = (props) => (
    <TouchableOpacity style={[props.style,{height:40, backgroundColor:Asset.color_skyblue, justifyContent:'center'}]} onPress={props.onPress}>
        <Text style={{color:'white', alignSelf:'center', fontSize:20, fontWeight:'bold'}}>{props.title}</Text>
    </TouchableOpacity>
);

export MyButton3;

【问题讨论】:

标签: reactjs react-native


【解决方案1】:

你可以。您的问题更多关于如何在一个文件中导出多个函数的语法。您有 2 种语法选择:

const a = 1
const b = 2
export { a, b }

export const a = 1
export const b = 1

然后导入到文件中

import { a, b } from 'some directory'

【讨论】:

  • 啊,你是对的!谢谢。我会在 10 分钟后将其标记为答案。我还有一个问题。当我使用'export const MyButton...'时,我可以在哪里放置'props type'?
  • MyButton.propTypes = {}
  • @BrightLee 您正在接受类型检查,对吧? reactjs.org/docs/typechecking-with-proptypes.html
  • 啊............即使我删除了'export default MyButton'语句,我仍然可以这样做。谢谢!
猜你喜欢
  • 2021-04-23
  • 2019-02-14
  • 2021-04-28
  • 1970-01-01
  • 2012-12-18
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
相关资源
最近更新 更多