【发布时间】:2020-07-29 15:59:20
【问题描述】:
我创建了一个自定义组件:
import React from 'react';
import {View, StyleSheet, TouchableOpacity} from 'react-native';
const Square = ({size, color, onPress, children}) => {
return (
<TouchableOpacity onPress={onPress}>
<View style={styles.sqr}>{children}</View>
</TouchableOpacity>
);
};
const styles = StyleSheet.create({
sqr: {
width: this.size,
height: this.size,
backgroundColor: this.color,
...
},
});
export default Square;
正如您在上面的sqr 样式中看到的,我尝试使用通过props 传入的size 和color。
我通过以下方式在另一个组件中使用Square:
<Square size={30} color="black" .../>
但是在运行我的应用程序时不会应用大小和颜色。
那么,如何在自定义组件的样式中使用传入的值?
【问题讨论】:
标签: react-native react-native-stylesheet react-native-component