我有这个:
import styled from 'react-emotion';
const Box = styled('div')`
display: flex;
flex-direction: ${p => p.direction};
`;
Box.defaultProps = {
direction: 'column'
};
当我使用 Box 组件时,这工作得很好。默认道具如预期的那样存在。
但是,当我重用带有样式的 Box 时,不会传递默认道具:
import styled from 'react-emotion';
import Box from './Box';
export const UniqResponsiveBox = styled(Box)`
// some media queries and other new styles
`;
当我使用 UniqResponsiveBox 时,它没有我为 Box 声明的 defaultProps。以下解决方法让我通过,但似乎多余,我相信我错过了仅使用情感来完成此任务的东西。
import styled from 'react-emotion';
const BoxContainer = styled('div')`
display: flex;
flex-direction: ${p => p.direction};
`;
function Box(props) {
return <BoxContainer {...props}/>
}
Box.defaultProps = {
direction: 'column'
}
import styled from 'react-emotion';
import Box from './Box';
export const UniqResponsiveBox = styled(Box)`
// some responsive queries and other uniq styles
`;
// In this scenario, the default props are there because I passed them explicitly. Helppp!