question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Theming with styled-components

See original GitHub issue

I’ve described the problem here.

I try to inject styled-components theme to every page but I only get empty theme Object prop passed.

import App, { Container } from "next/app"; // eslint-disable-line
import React from "react";
import { ThemeProvider } from "styled-components";

export default class MyApp extends App {
  static async getInitialProps({ Component, ctx }) {
    let pageProps = {};

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }

    return { pageProps };
  }

  render() {
    const { Component, pageProps } = this.props;
    return (
      <Container>
        <ThemeProvider theme={{ color: 'blue' }}>
          <Component {...pageProps} />
        </ThemeProvider>
      </Container>
    );
  }
}

Inside some page:

const A = styled.a`
  color: ${props => {
    console.log(props.theme); // "{}"
    return "blue";
  }};
`;

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

19reactions
xiaoyu-tamucommented, Apr 22, 2018

Now it works in 6.0.0-canary.4

// _app.js
import App, { Container } from 'next/app';
import React from 'react';
import { ThemeProvider } from 'styled-components';

const theme = {
  color: 'blue',
};

export default class MyApp extends App {
  static async getInitialProps({ Component, router, ctx }) {
    let pageProps = {};

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }

    return { pageProps };
  }

  render() {
    const { Component, pageProps } = this.props;
    return (
      <Container>
        <ThemeProvider theme={theme}>
          <Component {...pageProps} />
        </ThemeProvider>
      </Container>
    );
  }
}


// dir/component.js 
const Paragraph = styled.p`
  color: ${(p) => {
    console.log(p.theme);       // { color: 'blue' }
    return p.theme.color;
  }};
`;
14reactions
Inlustracommented, Jun 4, 2019

Also have the same issue with 8.1.0, working fine in 8.0.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Usage - styled-components
styled -components has full theming support by exporting a <ThemeProvider> wrapper component. This component provides a theme to all React components ...
Read more >
Theming and Theme Switching with React and styled ...
styled -components: A flexible way to style React components with CSS. It provides out-of-the-box theming support using a wrapper component ...
Read more >
How to use Themes in styled-components - DEV Community ‍ ‍
Theming is a great tool to use within front-end applications. It also allows me to write way less code and be more consistent...
Read more >
Adding themes to a React app using styled-components | Blog
Styles are scoped: unlike CSS classes, which are global and affect your entire document, styled-components styles are specific to your component ...
Read more >
Build a React theme switcher app with styled-components
Efficiency: styled-components uses the React Context API which offers a Theme Context that we can pass a theme into as a prop, allowing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found