在样式组件上使用.propTypes和.defaultProps可以吗?

时间:2020-04-11 00:05:50

标签: javascript reactjs styled-components react-proptypes

可以直接在样式化的组件上定义propTypes和defaultProps吗?否则,我必须为默认导出命名。我只是想保持原样。组件:

import React from "react";
import PropTypes from "prop-types";
import styled from "styled-components";

/**
 * This component renders a div with a specified color.
 */

export default ({ color, children }) => <Box color={color}>{children}</Box>;

const Box = styled.div`
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 4px;

  color: white;
  font-weight: bold;
  font-size: 1.2rem;

  background-color: ${({ color }) => color};
  margin: 6px;
`;

Box.propTypes = {
  color: PropTypes.string,
}

Box.defaultProps = {
  color: "grey",
}

0 个答案:

没有答案