我创建了一个自定义组件:
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
样式中看到的那样,我尝试使用size
andcolor
传入 via props
。
我通过Square
以下方式在另一个组件中使用:
<Square size={30} color="black" .../>
但是,运行我的应用程序时不会应用大小和颜色。
那么,如何在自定义组件的样式中使用传入的值?