injectGlobal失效
styled-components从4版本以上就废弃了injectGlobal 方法,使用createGlobalStyle进行替换处理;
使用createGlobalStyle
定义样式js文件
import { createGlobalStyle } from 'styled-components'
export const GlobalStyle=createGlobalStyle
`
body{
margin:0;
padding:0;
}
`
在组件中进行使用
import React, { Component } from 'react';
import {GlobalStyle} from './index_css.js';
class App extends Component {
render() {
return (
<div className="App">
<GlobalStyle/>
hello world
</div>
);
}
}
export default App;
将引入的GlobalStyle作为一个组件跟在根组件下,就可以了