reactjs - 为了可读性,将 defaultProps 放在无状态组件定义之前
问题描述
为了可读性,有没有办法将contextTypes和defaultProps放在无状态的 React 组件之前?
type TopBarProps = {
children: string,
color: string
};
TopBar.defaultProps = {
color: "white"
};
TopBar.contextTypes = {
locale: PropTypes.string,
colors: PropTypes.object,
i18n: PropTypes.object,
fontFamily: PropTypes.object,
scale: PropTypes.number,
alignStyle: PropTypes.string,
navMethods: PropTypes.object,
token: PropTypes.string,
arrowName: PropTypes.string,
materialArrows: PropTypes.object
};
export const TopBar = (props: TopBarProps, context) => {
const { colors } = context;
styles = styles || initStyles(context);
return (
<View style={styles.container}>
<View>
<Menu color={colors.colorFont} />
</View>
<View>
<TopLabel color={props.color}>{props.children}</TopLabel>
</View>
</View>
);
};
解决方案
您可以先将它们定义为常量,然后在定义后将它们设置为组件的字段。由于它们的名称恰当,读者应该很清楚这些常量的用途。
const defaultProps = { ... }
const contextTypes = { ... }
export const TopBar = (props: TopBarProps, context) => { ... }
TopBar.defaultProps = defaultProps
TopBar.contextTypes = contextTypes
推荐阅读
- powershell - 如何使用 powershell 在应用程序模式下启动 Chromium Edge selenium webdriver
- python - 如何在 Jupyter 中修复“没有名为“blank_blank”的模块?
- regex - bigquery 的负前瞻替代方案
- javascript - 如何在 InpuTime 中传递带有参数的删除函数?
- webassembly - 是否可以在普通页面中嵌入跨域隔离的 iframe?
- android - 是否可以在应用程序类中删除房间数据库的数据?
- flutter - 我怎样才能在我的数据表中设计出这种标题或标题?
- spring-boot - 微服务中的事务管理
- ceph - Rook Ceph 配置问题
- javascript - 获取表单以将选定的选项发送到电子邮件