Unable to apply styles to styled-component element in Reactjs

Viewed 307

I'm trying to wrap a component with a styled-component div and apply some styles. However, for some reason, the styles aren't being applied, even though the ostensibly correct wrapper that is expected to be rendered is being rendred.

CodeSandbox

What am I doing wrong here?

2 Answers

You need to container Component with styled.{tag} or styled(Component) And then use that Container component to wrap main component.

import React from "react";
import "./styles.css";
import styled from "styled-components";

export default () => <Component />;

const Component = () => <StyledDiv>Hello World!</StyledDiv>;

const StyledDiv = styled.div`
  font-size: 10rem;
  color: red;
  display: flex;
  justify-content: center;
  align-items: center;
`;

Check here: https://codesandbox.io/s/dank-hill-h8rbc?file=/src/App.js:128-189

Related