【发布时间】:2021-07-31 02:45:14
【问题描述】:
显示的是 fontSize: 20 和 color: 'red' 属性,而不是从 Parent 组件设置的 fontSize: 12 和 color: 'blue',但 fontWeight: 'bold' 正确显示。
我有一个可重用的 React UI 子组件,它像这样从其父组件接收一些道具
import React from "react";
import { Text, StyleSheet } from "react-native";
const BodyText = ( props ) => {
return (
<Text style={styles.bodyText} {...props} >{props.children}</Text>
);
};
const styles = StyleSheet.create({
bodyText: {
fontSize: 20,
color: 'red'
},
});
export default BodyText;
父组件长这样
import React from "react";
import { StyleSheet } from "react-native";
import BodyText from './components/UI/BodyText'
const Parent = () => {
return (
<BodyText style={styles.text} >Hi There</BodyText>
);
};
const styles = StyleSheet.create({
text: {
fontSize: 12,
color: 'blue',
fontWeight: 'bold'
},
});
export default Parent;
有趣的是,当我在CodePen 中玩耍时,它可以正确显示,但在我的本地设置中却没有。
【问题讨论】:
标签: javascript reactjs react-native react-props