Image for post
Image for post

Releasing styled-components v4 final 🎊

After a couple weeks of beta testing styled-components v4 is now officially ready for primetime!

Evan Jacobs
Oct 15, 2018 · 2 min read

If you didn’t read , here’s a quick primer on what’s in this release:

  • Smaller and much, much faster
  • A refreshed createGlobalStyle API, replacing the old injectGlobal
  • Support for the "as" prop, a dynamic alternative to .withComponent
  • Removal of Comp.extend in favor of unifying styled(Comp)
  • Full compliance for React v16
  • Native support for ref on any styled component

Read for the full details on these.

That’s a lot already, but we haven’t been sleeping since the initial announcement! Here’s what new in the final release:

  • A brand-new so you can get all the goodies of the Babel plugin in create-react-app! 🎣
  • Fully automatic to auto-magically migrate most of your codebase from to v4 🎩✨
  • A test util for Enzyme so that unit testing your components is easier
  • All the bugfixes since the initial beta release (shoutout to all the early testers for their invaluable feedback)

Announcing the styled-components Babel Macro 🎣

Thanks to our contributor Luc Leray ! This means create-react-app users can get all the benefits of the styled-components (most notably, nicer class names) without needing to eject 🎉

All you have to do is change your import statements to "styled-components/macro" and that’s it:

import styled from "styled-components";👇👇👇👇👇import styled from "styled-components/macro";

5 Minute Migration 🎩✨

We’ve worked hard on making this upgrade as easy as possible, so for most of you there’s only three steps to upgrade your entire codebase to v4:

  1. Install the latest styled-components version by running npm install styled-components@latest babel-plugin-styled-components@latest
  2. Install globally with npm install -g styled-components-codemods
  3. Run the codemods on your codebase with styled-components-codemods v4 src/**/*.js. They will auto-magically move all your Comp.extend usages to styled(Comp) and your injectGlobal usages to createGlobalStyle!

If you’re using TypeScript, you’ll also need to upgrade your typings to the new ones from DefinitelyTyped: npm install @types/styled-components@latest

Note: see and potential edge cases, and make sure that you’re on React v16.3+, which is required for styled-components v4 due to the new ref system.

From all of us, we hope you love v4 as much as we loved working on it! Let us know what you think in the community:

As always, stay stylish. ✌️💅

Like styled-components? Please consider making a donation or convincing your company to sponsor our ! We’re a typical open source volunteer team and your contribution helps us do things like compensate collaborators, go to conferences, and make cool swag.

💅 styled-components

Visual primitives for the component age.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface.

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox.

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store