简体   繁体   English

React-Emotion:将defaultProps传递给组件重用

[英]React-Emotion: pass down defaultProps to in component reuse

I have this: 我有这个:

 import styled from 'react-emotion'; const Box = styled('div')` display: flex; flex-direction: ${p => p.direction}; `; Box.defaultProps = { direction: 'column' }; 

This works just fine when I use the Box component. 当我使用Box组件时,这很好用。 The default props are there as expected. 默认道具符合预期。

However, when I reuse Box with styled, the default props do not get passed down: 但是,当我重复使用带有样式的Box时,默认道具不会传递下去:

 import styled from 'react-emotion'; import Box from './Box'; export const UniqResponsiveBox = styled(Box)` // some media queries and other new styles `; 

When I use UniqResponsiveBox, it does not have the defaultProps I declared for Box. 当我使用UniqResponsiveBox时,它没有我为Box声明的defaultProps。 The following workaround gets me thru, but seems redundant and I believe Im missing something that accomplishes this only using emotion. 以下解决方法让我通过,但似乎多余,我相信我错过了一些只使用情感来实现这一点。

 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! 

This particular issue is a bug inside Emotion - it was fixed in a pull request that was merged 11 days ago, so it should appear in the next release. 这个特殊问题是Emotion中的一个错误 - 它是在11天前合并的pull请求中修复的,所以它应该出现在下一个版本中。

In the meantime, another workaround to avoid the additional function would simply be: 与此同时,避免附加功能的另一种解决方法就是:

import styled from 'react-emotion';
import Box from './Box';

export const UniqResponsiveBox = styled(Box)`
  // some media queries and other new styles
`;

UniqResponsiveBox.defaultProps = Box.defaultProps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM