[英]How do I pass complex styles (i.e. stylesheets) to React components as props?
I'm working on a large React project where each member of the team has been making components and stylesheets separately. 我正在一个大型的React项目中,团队的每个成员都在分别制作组件和样式表。 I'm trying to find the common elements and re-write the code, creating re-usable components.
我试图找到常见的元素并重新编写代码,创建可重复使用的组件。 At the moment each of these components has a stylesheet -- SCSS -- already written.
目前,每个组件都有一个样式表-SCSS-已编写。
What I'd like to do is be able to pass styles to the component so that it can be customised (somewhat) in different locations. 我想做的是能够将样式传递给组件,以便可以在不同位置进行自定义(某种程度上)。 I know how to do this for the top-level HTML element in the component
我知道如何对组件中的顶级HTML元素执行此操作
export default class BoxWithSliderAndChevron extends Component {
render() {
const {
props: {
styles
},
} = this;
return (
<div className="BoxWithSliderAndChevron-main" style={styles}>
but as I understand it, these styles will only apply to this outer div? 但是据我了解,这些样式仅适用于该外部div吗? How can I pass styles such that I can re-style elements further down in the component's structure, using their
classNames
? 如何传递样式,以便可以使用组件的
classNames
在组件的结构中进一步对元素进行样式设置? As if I were passing a new stylesheet that would override the default stylesheet? 好像我正在传递一个将覆盖默认样式表的新样式表?
I suppose I could pass a number of style objects, but that seems cumbersome -- I'm wondering if there is a simpler way? 我想我可以传递许多样式对象,但这似乎很麻烦-我想知道是否有更简单的方法?
What you are trying to achieve kinda goes against the whole idea of inline styles (non-global, non-separated from implementation, etc), however you are right, passing a style
prop and trying to apply it to a div will inmediatly result to only the parent having the styles applied. 您想要实现的目标与内联样式(非全局,与实现分离等)的整体思想背道而驰,但是您是对的,传递
style
支持并将其应用于div会立即导致仅父级应用了样式。
One suggestion would be to merge the component styles with the props, ex: 一种建议是将组件样式与道具合并,例如:
import { StyleSheet } from 'react-native';
class Foo extends React.PureComponent {
render() {
return (
<div style={StyleSheet.merge([styles.parentStyle, styles.parentStyle])}>
<div style={StyleSheet.merge([styles.childStyle, styles.childStyle])}>
</div>
)
}
}
const styles = StyleSheet.create({
parentStyle: {
backgroundColor: 'red'
},
childStyle: {
backgroundColor: 'blue'
}
});
It is tedious work, but it is basically what you are trying to achieve, another approach is having theming globally applied: 这是一项繁琐的工作,但基本上是您要实现的目标,另一种方法是在全球范围内应用主题:
import { StyleSheet } from 'react-native';
import { t } from '../theming'; // <- You switch themes on runtime
class Foo extends React.PureComponent {
render() {
return (
<div style={StyleSheet.merge([styles.parentStyle, t().parentStyle])}>
<div style={StyleSheet.merge([styles.childStyle, t().childStyle])}/>
</div>
)
}
}
const styles = StyleSheet.create({
parentStyle: {
backgroundColor: 'red'
},
childStyle: {
backgroundColor: 'blue'
}
});
/// Theming file would be something like:
// PSEUDO IMPLEMENTATION
import theme1 from 'theme1.json';
import theme2 from 'theme2.json';
availableThemes = {
theme1,
theme2
}
currentTheme = availableThemes.theme1
function setTheme(theme) {
currentTheme = availableThemes[theme]
}
export function t() {
return current theme
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.