React Styled Components-用道具变换比例

时间:2019-11-09 16:36:43

标签: reactjs styled-components

我正在尝试设置导航栏的样式,我希望菜单过渡到打开状态。我试图通过将一个prop传递给样式化组件,然后基于该prop设置transform的值来实现此目的,但是我看不出要弄清楚在一个带样式的prop中使用prop的语法。这是代码:

<MenuContainer display={showMobileMenu ? '' : 'none'}>
  <UL>
    <LI><A showMobileMenu={showMobileMenu} href="#">Sign in with Github</A></LI>
    <LI><A showMobileMenu={showMobileMenu} href="#">New Snippet</A></LI>
  </UL>
</MenuContainer>
.
.
.
const MenuContainer = styled.nav`
  position: absolute;
  text-align: right;
  top: 100%;
  background: ${colors.headerBackground};
  width: 100%;
  display: ${props => props.display};
  transform: ${props => props.display === 'none' ? scale(1,1) : scale(1,0)};
  transform-origin: top;
  transition: transform 500ms ease-in-out;
`;

React抱怨“标尺未定义为un-undef”。

2 个答案:

答案 0 :(得分:0)

您会收到此错误,因为JavaScript表达式中的scaleundefined
您必须在javascript expression中将CSS作为字符串编写。
例如这样的:

  transform: ${props => props.display === 'none' ? `scale(0.7)` : `scale(1,1)`};

我在代码沙箱中为您提供了一个简单的代码: https://codesandbox.io/s/musing-dirac-tb08v
更多资源:
https://www.styled-components.com/docs/basics#adapting-based-on-props

答案 1 :(得分:0)

与此问题相关,在像这样的规模内使用道具。

m = df.groupby(['id', 'y', df['y'].cumsum()]).cumcount() + 1                 ########## setup you base getting cumcount
df['num_month'] = np.where((m == 1) & (m.shift() > 1), m.shift() + 1, m).astype(int)  # extend cumcount one further row per group for first line of code
s1 = df.groupby('id').transform('idxmin').iloc[:,0]                      ############## get index location of first value per group and return as series with same length
s2 = df.groupby(['id', (df['y'] > 0).cumsum()]).transform('idxmin').iloc[:,0] ######### get index location of first non-zero value per group and return as series with same length
df['num_month'] = df['num_month'].mask((s1 == s2) | (s1 == s2.shift()), -1) ########### using s1 and s2 conditions, update the necessary rows to -1
df
Out[1]: 
    id  year  month  y  sinx  num_month
0    1  2019      1  0     1         -1
1    1  2019      2  0     2         -1
2    1  2019      3  1     3         -1
3    1  2019      4  0     4          1
4    1  2019      5  0     5          2
5    1  2019      6  0     6          3
6    1  2019      7  0     7          4
7    1  2019      8  2     8          5
8    1  2019      9  0     9          1
9    1  2019     10  0    10          2
10   1  2019     11  0    11          3
11   1  2019     12  0    11          4
12   1  2020      1  0    12          5
13   1  2020      2  0    13          6
14   1  2020      3  2    14          7
15   1  2020      4  0    15          1
16   2  2019      1  0     1         -1
17   2  2019      2  0     2         -1
18   2  2019      3  1     3         -1
19   2  2019      4  0     4          1