Styled-components--V4 injectGlobal的弃用

注:在styled-component V4版本中injectGlobal API除去,取而代之的是createGlobalStyle样式组件

也就是说以前的 injectGlobal 全局样式在V4版本代替为使用 createGlobalStyle 渲染组件的方式来使用全局样式。


官方文档说明(地址:https://www.styled-components.com):

A helper method to write global CSS. It does not return a component, but adds the styles to the stylesheet directly.

ARGUMENTSDESCRIPTION
1. TaggedTemplateLiteralA tagged template literal with your global styles inside.
import { injectGlobal } from 'styled-components' 

injectGlobal` 
@font-face { 
    font-family: "Operator Mono"; 
    src: url("../fonts/Operator-Mono.ttf"); 
} 
body { 
    margin: 0; 
} 
` 
We do not encourage the use of this. Try to use it once per app at most, if you must, contained in a single file. This is an escape hatch. Only use it for the rare @font-face definition or body styling.

createGlobalStyle 官方说明:

A helper function to generate a special StyledComponent that handles global styles. Normally, styled components are automatically scoped to a local CSS class and therefore isolated from other components. In the case of createGlobalStyle, this limitation is removed and things like CSS resets or base stylesheets can be applied.

ARGUMENTSDESCRIPTION
1. TaggedTemplateLiteralA tagged template literal with your CSS and interpolations.

Returns a StyledComponent that does not accept children. Place it at the top of your React tree and the global styles will be injected when the component is "rendered".

import { createGlobalStyle } from 'styled-components'

const GlobalStyle = createGlobalStyle`
  body {
    color: ${props => (props.whiteColor ? 'white' : 'black')};
  }
`

// later in your app

<React.Fragment>
  <Navigation /> {/* example of other top-level stuff */}
  <GlobalStyle whiteColor />
</React.Fragment>
Since the GlobalStyle component is a StyledComponent, that means it also has access to theming from the <ThemeProvider> component if provided.
import { createGlobalStyle, ThemeProvider } from 'styled-components'

const GlobalStyle = createGlobalStyle`
  body {
    color: ${props => (props.whiteColor ? 'white' : 'black')};
    font-family: ${props => props.theme.fontFamily};
  }
`

// later in your app

<ThemeProvider theme={{ fontFamily: 'Helvetica Neue' }}>
  <React.Fragment>
    <Navigation /> {/* example of other top-level stuff */}
    <GlobalStyle whiteColor />
  </React.Fragment>
</ThemeProvider>

实例测试:

style.js :

import {createGlobalStyle} from 'styled-components';

export const GlobalStyle = createGlobalStyle`
    body {
        background: blue;
    }
`;

App.js:

import React, { Component,Fragment } from 'react';
import Header from "./common/header";
//导入样式文件
import {GlobalStyle} from "./style";

class App extends Component {
    render() {
        return (
            <Fragment>
                <GlobalStyle/>
                    <Header/>
            </Fragment>
        )
    }
}

export default App;

做的不好,仅供参考!

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值