Aar*_*ton 7 javascript ecmascript-6 reactjs styled-components
我正在使用样式组件并使用其标记的模板文字语法生成组件,例如:
const Button = styled.button`
background-color: papayawhip;
border-radius: 3px;
color: palevioletred;
`
Run Code Online (Sandbox Code Playgroud)
在一种情况下,我需要调用一个函数,该函数基于断点生成媒体查询,并传递要包含在其中的标记的css模板文字.
例如:
media(12)`
background-color: papayawhip;
`
Run Code Online (Sandbox Code Playgroud)
媒体功能可能如下所示:
const media = mapValues(width => ({ css: (...args) => css`
@media (min-width: ${width}rem) {
${css(...args)}
}
`}));
Run Code Online (Sandbox Code Playgroud)
是否可以传递值和标记的模板文字,或者我是否以错误的方式进行此操作?
标记的模板文字不是魔术,你只需要从你的media(12)调用中返回另一个函数:
function media(twelve) {
return function(stringParts, ...interpolationValues) {
return …
}
}
Run Code Online (Sandbox Code Playgroud)
或使用箭头函数
const media = (twelve) => (stringParts, ...interpolationValues) => …;
Run Code Online (Sandbox Code Playgroud)
被称为
media(12)`firstPart ${13} secondPart`
// or equvialently
media(12)(["firstPart ", " secondPart"], 13)
Run Code Online (Sandbox Code Playgroud)
但是,如果您不需要进行任何插值而只想接收一个字符串,那么编写带有参数的函数可能会更容易
function media(twelve, string) {
return …;
}
Run Code Online (Sandbox Code Playgroud)
并将其称为
media(12, `
templateString
`)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
862 次 |
| 最近记录: |