1

I started rewriting CSS into React Components but i've encountered a problem, I do know how to do this (using styled-components btw ) : enter image description here

3
  • Styled component is prop based. You can pass title and description as boolean props. Commented Jan 11, 2020 at 20:33
  • Can u gimme some link with an example please? Commented Jan 11, 2020 at 20:48
  • Welcome to Stack Overflow. The Styled Components web site has an intro page for you to look at. styled-components.com/docs/basics#getting-started particularly the section on "Pseudoelements, pseudoselectors, and nesting" Commented Jan 11, 2020 at 21:00

2 Answers 2

1

You have 5 ways to styling component in React.

Every approach have pros & cons(personally I use 4-th ways.)

1.Inline Css

const divStyle = {
  color: 'blue',
  backgroundImage: 'url(' + imgUrl + ')',
};
function HelloWorldComponent() {
  return <div style={divStyle}>Hello World!</div>;

2. CSS in JS

const Button = (props) => (
  <button className={ 'large' in props && 'large' }>
     { props.children }
     <style jsx>{`
        button {
          padding: 20px;
          background: #eee;
          color: #999
        }
        .large {
          padding: 50px
        }
     `}</style>
  </button>
)

/* Creates a regular button */
<Button>Hi</Button>

/* Creates a large button */
<Button large>Big</Button>

3. Styled Components

const Wrapper = styled.section`
  padding: 4em;
  background: papayawhip;
`;


render(
  <Wrapper>
    <Title>
      Hello World!
    </Title>
  </Wrapper>
);

4. Css Modules (scss || sass)

@width: 10px;
@height: @width + 10px;
#header {
  width: @width;
  height: @height;
}

5. Stylable - https://github.com/wix/stylable

Sign up to request clarification or add additional context in comments.

Comments

0
import React from 'react';
import styled, {css} from 'styled-components';

const Info = styled.div`
display: flex;
// etc.
${props => props.title && css`font-size: 15px`}
`
const Box = styled.div`
&:first-of-type {
// some more css 
}
`
// usage
<Info title>some info</Info>

I recommend you follow the official docs as stated in comments by @Mikkel

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.