我是 React 和 React Native 的新手。目前,对于每个组件,我将代码分成两个单独的文件:
-
index.js
对于所有 React 代码,以及;
-
styles.js
对于样式表
有没有办法将 props 传递到外部样式表中?
例子:index.js
:
render() {
const iconColor = this.props.color || '#000';
const iconSize = this.props.size || 25;
return (
<Icon style={styles.icon} />
);
}
Example styles.js
:
const styles = StyleSheet.create({
icon : {
color: iconColor,
fontSize: iconSize
}
});
上面的代码不起作用,但它更多的是为了说明我正在尝试做的事情。任何帮助深表感谢!
我宁愿将我的样式放在单独的文件 styles.js 中。
styles.js 内部:
export const styles = (props) => StyleSheet.create({
icon : {
color: props.iconColor,
fontSize: props.iconSize
}
}
在你的主类中你可以传递这个值
return (
<Icon style={styles(this.props).icon} />
);
或者你可以直接这些值
所以它会是
export const styles = (iconColor,iconSize) => StyleSheet.create({
icon : {
color: iconColor,
fontSize: iconSize
}
}
在你的主类中
return (
<Icon style={styles(this.props,iconColor,
this.props.iconSize).icon} />
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)