sections/faqs/migration-v4.mdx
Historical note: This migration guide is for upgrading from v3 to v4 (released 2018). If you're on a recent version, see the v6 migration guide instead.
This is a pretty big release with lots of changes both under the hood and at the API level.
npm install styled-components@^4.0.0
react >= 16.3; internally we are using the new React.forwardRef API and new context APIs if you wish to try and polyfill for older React version supportnpm install react@^16.3 react-dom@^16.3
If you are using
enzymeor other dependencies likereact-test-renderer, there may be more related upgrades to complete if you are coming from an old version ofreact.
.extend API, switch your components to use styled(StyledComponent) instead.A codemod is available to expedite this.
š«
import styled from 'styled-components'
const Component = styled.div`
background: blue;
color: red;
`
const ExtendedComponent = Component.extend`
color: green;
`
ā
import styled from 'styled-components'
const Component = styled.div`
background: blue;
color: red;
`
const ExtendedComponent = styled(Component)`
color: green;
`
See the "extending styles" documentation for more examples.
injectGlobal API to add global styles to your page, use the new createGlobalStyle helper component instead.A codemod is available to expedite this.
š«
import { injectGlobal } from 'styled-components'
injectGlobal`
body {
color: red;
}
`
ā
import { createGlobalStyle } from "styled-components"
const GlobalStyle = createGlobalStyle`
body {
color: red;
}
`
// later in your app's render method
<React.Fragment>
<Navigation />
<OtherImportantTopLevelComponentStuff />
<GlobalStyle />
</React.Fragment>
See the documentation for createGlobalStyle to see all the cool stuff you can do with it that wasn't possible before with injectGlobal!
innerRef prop, change it to a normal ref.š«
const Component = styled.div`
background: blue;
color: red;
`
// later in your render method
<Component innerRef={element => { this.myElement = element }}>Something something</Component>
ā
const Component = styled.div`
background: blue;
color: red;
`
// later in your render method
<Component ref={element => { this.myElement = element }}>Something something</Component>
keyframes component in a partial without the css helper, you'll need to use the helper now. In general, always use the css helper when composing styling partials to be interpolated into a styled component.š«
import styled, { keyframes } from 'styled-components'
const animation = keyframes`
0% {
opacity: 0;
}
100 {
opacity: 1;
}
`
const animationRule = `
${animation} 1s infinite alternate
`
const Component = styled.div`
animation: ${animationRule};
`
ā
import styled, { css, keyframes } from 'styled-components'
const animation = keyframes`
0% {
opacity: 0;
}
100 {
opacity: 1;
}
`
const animationRule = css`
${animation} 1s infinite alternate;
`
const Component = styled.div`
animation: ${animationRule};
`
attrs({}) and some of the attributes you pass to it is a Function, it's recommended to switch to the new attrs(props => ({})) syntax instead for easier and more powerful composition.š«
import styled from 'styled-components'
const Input = styled.input.attrs({
type: props => props.inputType,
})`
background: blue;
color: red;
`
ā
import styled from 'styled-components'
const Input = styled.input.attrs(props => ({
type: props.inputType,
}))`
background: blue;
color: red;
`
@types/styled-components needed).That's it! Aside from migrating, we also highly recommend reading up on the "as" prop which replaced the withComponent API (removed in v6).