简体   繁体   English

将内联样式更改为样式组件

[英]Changing inline style into styled-component

I am trying to switch from inline styling to css-in-jsx.我正在尝试从内联样式切换到 css-in-jsx。 I found out about styled-components and was hoping that If I use exactly the same styling features, I would get the same results in both cases.我发现了 styled-components 并希望如果我使用完全相同的样式特性,我会在两种情况下得到相同的结果。 However, that doesn't seem to be the case.然而,情况似乎并非如此。

For example,例如,

<Container component="main" maxWidth="xs">
      <CssBaseline />
      <div style={{
               display: 'flex',
               flexDirection: 'column',
               alignItems: 'center'
             }}>
      {/* <Wrapper> */}
        <Avatar>
          <LockOutlinedIcon />
        </Avatar>
        <Typography component="h1" variant="h5">
          Sign in
        </Typography>
..................
      {/* </Wrapper> */}
      </div>
    </Container>
  );
}

when I use this, everything works well.当我使用它时,一切正常。 在此处输入图片说明 However, now I have made this in another file:但是,现在我在另一个文件中做了这个:

export const Wrapper = styled.div`
  display: flex;
  flex-direction: 'column';
  align-items: center;
`;

When I comment out my old div and use the Wrapper instead of that, I get this:当我注释掉我的旧div并使用Wrapper而不是它时,我得到了这个: 在此处输入图片说明

Why is this so?为什么会这样? If the styling commands are same, why don't I get the same results?如果样式命令相同,为什么我得不到相同的结果? Does this mean, I will have to fix all components if I am moving to styled-components?这是否意味着,如果我转向样式组件,我将不得不修复所有组件?

Also, how can I fix this particular case?另外,我该如何解决这个特殊情况? :D :D

I agree with the comments, a sandbox would be nice.我同意评论,沙箱会很好。 I don't know if it will solve your issue, but usually it's a good practise to specify your styled props as strings without citation delimiters, ie我不知道它是否会解决您的问题,但通常将您的样式道具指定为没有引用分隔符的字符串是一个好习惯,即

export const Wrapper = styled.div`
  display: flex;
  flex-direction: column;
  align-items: center;
`;

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

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