W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
注意
的 。延伸API已在樣式組件v4中刪除。用styled(StyledComponent)代替。
這是一種創(chuàng)建新 StyledComponent 并擴(kuò)展其規(guī)則。
爭(zhēng)論 | 描述 |
---|---|
1。 TaggedTemplateLiteral | 帶CSS和插值的帶標(biāo)記的模板文字。 |
import styled from 'styled-components'
const Component = styled.div`
color: red;
`
const Component2 = Component.extend`
background: white;
color: blue;
`
返回一個(gè)新的 StyledComponent 將新規(guī)則合并到調(diào)用此方法的組件中。
注意
的 注入全局 API已刪除并替換為 createGlobalStyle 在樣式化組件v4中。
編寫(xiě)全局CSS的輔助方法。它不返回組件,而是將樣式直接添加到樣式表中。
爭(zhēng)論 | 描述 |
---|---|
1. TaggedTemplateLiteral | 帶標(biāo)記的模板文字,其中包含您的全局樣式。 |
import { injectGlobal } from 'styled-components'
injectGlobal`
@font-face {
font-family: "Operator Mono";
src: url("../fonts/Operator-Mono.ttf");
}
body {
margin: 0;
}
`
我們不鼓勵(lì)使用此功能。如果需要,請(qǐng)嘗試在每個(gè)應(yīng)用程序中最多使用一個(gè)文件。這是一個(gè)逃生艙口。僅用于稀有@ font-face 定義或身體樣式。
注意
的 “ innerRef”在樣式組件v4中刪除了prop以支持React 16forwardRefAPI。只要用正常參考 道具代替。
通過(guò) 參考 樣式組件的道具會(huì)給您一個(gè)實(shí)例 StyledComponent包裝器,但不包裝到基礎(chǔ)DOM節(jié)點(diǎn)。這是由于ref的工作方式。不可能調(diào)用DOM方法,例如焦點(diǎn),直接在我們的包裝上。
要獲得對(duì)實(shí)際包裝的DOM節(jié)點(diǎn)的引用,請(qǐng)將回調(diào)傳遞給 innerRef 道具代替。
注意
我們不支持字符串引用(即 innerRef =“ node”),因?yàn)樗鼈円言赗eact中棄用。
這個(gè)例子使用 innerRef 將ref保存到樣式輸入中,并在用戶(hù)將鼠標(biāo)懸停在其上時(shí)將其聚焦。
const Input = styled.input`
padding: 0.5em;
margin: 0.5em;
color: palevioletred;
background: papayawhip;
border: none;
border-radius: 3px;
`
class Form extends React.Component {
render() {
return (
<Input
placeholder="Hover here..."
innerRef={x => {
this.input = x
}}
onMouseEnter={() => this.input.focus()}
/>
)
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: