首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React-Emotion:在组件重用中向下传递defaultProps

React-Emotion:在组件重用中向下传递defaultProps
EN

Stack Overflow用户
提问于 2018-05-08 01:08:24
回答 1查看 1.3K关注 0票数 5

我有这个:

代码语言:javascript
复制
import styled from 'react-emotion';

const Box = styled('div')`
  display: flex;
  flex-direction: ${p => p.direction};
`;

Box.defaultProps = {
  direction: 'column'
};

当我使用Box组件时,它工作得很好。默认的道具如预期的那样在那里。

但是,当我使用styled重用Box时,默认的道具不会被传递:

代码语言:javascript
复制
import styled from 'react-emotion';
import Box from './Box';

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

当我使用UniqResponsiveBox时,它没有我为Box声明的defaultProps。下面的解决办法让我顺利通过,但似乎是多余的,我相信我错过了一些仅通过情感来完成这一点的东西。

代码语言:javascript
复制
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'
}

代码语言:javascript
复制
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!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-02 08:27:20

这个特殊的问题是情感内部的一个错误--它已经在11天前合并的a pull request中修复了,所以它应该会出现在下一个版本中。

同时,避免附加功能的另一种解决方法是:

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50219259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档