javascript - React:如何继承 PropTypes?
问题描述
我有一个 React Router Dom 和 Material UI 的包装器组件:
import Button from '@material-ui/core/Button';
import React from 'react';
import { Link as RouterLink } from 'react-router-dom';
const forwardedLink = React.forwardRef((props, ref) => (
<RouterLink innerRef={ref} {...props} />
));
function Link(props) {
return (
<Button component={forwardedLink} {...props}>
{props.children}
</Button>
);
}
Link.propTypes = // ?
export default Link;
我怎样才能给出Link
Prop-Types from Button
?
解决方案
请参阅this other SO answer,以下应该可以。
/**
* @type React.ForwardRefRenderFunction<React.FunctionComponent, ButtonPropTypes>
*/
const Button = forwardRef(({ text, icon }, ref) => (
<button ref={ref}>{text}{icon}</button>
))
const ButtonPropTypes = {
text: string.isRequired,
icon: string.isRequired,
}
Button.propTypes = ButtonPropTypes
推荐阅读
- python - 一种在 GL_EDGES 上停止颜色插值的方法?
- html - 使用 vba 在 html 的自动完成下拉列表中选择一个项目
- java - 如何单击 id 在页面刷新时不断变化的日期选择器
- spring-boot - 上游连接错误或在标头之前断开/重置。重置原因:使用 Spring Boot 时连接终止
- sockets - websockex:使用 CRLF json 协议连接 ssl 套接字时出错
- python - 蟒蛇 | 使用子类对象从父类访问变量是否比从子类访问相同变量慢?
- java - 为什么自定义@Exclude 不从序列化中排除字段
- mailgun - 通过 Mailgun 向手机发送电子邮件不起作用
- excel - 数字格式在 VBA 中无法正常工作
- node.js - 生产服务器上 docker 环境中的 Node.js 内存泄漏