Styled-components

2021-10-18  本文已影响0人  rangel

styled-components https://styled-components.com/

什么是styled-components?

styled-components is the result of wondering how we could enhance CSS for styling React component systems. By focusing on a single use case we managed to optimize the experience for developers as well as the output for end users.

怎么用?

// Create a Title component that'll render an <h1> tag with some styles
const Title = styled.h1`
  font-size: 1.5em;
  text-align: center;
  color: palevioletred;
`;

// Create a Wrapper component that'll render a <section> tag with some styles
const Wrapper = styled.section`
  padding: 4em;
  background: papayawhip;
`;

// Use Title and Wrapper like any other React component – except they're styled!
render(
  <Wrapper>
    <Title>
      Hello World!
    </Title>
  </Wrapper>
);

The preprocessor we use, stylis, supports scss-like syntax for automatically nesting styles.

image.png

浏览器上展示的

image.png

每个对应的Styled component都会被解析成一个唯一的classname,避免重复

Props:

Cons:

One way to ease this issue slightly is utilizing code-splitting in your app (check out react-loadable or React Code Splitting ) - because styled-components happens in JS land, code-splitting ensures only the styles & javascript that are necessary for the current page are sent down to the browser, and the rest can be lazily fetched as the user navigates around.

上一篇下一篇

猜你喜欢

热点阅读