【发布时间】:2020-11-04 17:26:17
【问题描述】:
我想访问我从资产文件调用的对象内的对象属性:
const services = {
facebook: {
name: "facebook",
color: "#28A2F3"
},
spotify: {
name: "spotify",
color: "#42D778"
}
}
module.exports = services
我的组件:
export default function PasswordCard({icon, color}, props) {
return (
<View style={{
padding: 20,
backgroundColor: service[props.site].color,
elevation: 2,
borderRadius: 5,
flexDirection: "row",
alignItems: "center",
justifyContent: "space-evenly",
marginTop:5,
marginBottom: 5,
}}>
<Icon style={styles.service} name={service[props.site].name} size={27} color="white" />
<View style={styles.TextContainer}>
<Text style={styles.Text}>k.adesida@provi.data</Text>
<Text style={styles.Text}>+213 (0) 384639293</Text>
</View>
<TouchableHighlight style={styles.icon}>
<BenjaminButton name="eye" size={22} color="white" />
</TouchableHighlight>
</View>
)
}
我用传递的道具调用我的组件:
<View style={styles.CardContainer}>
<PasswordCard site={"facebook"} />
<PasswordCard site={"spotify"} />
</View>
我得到的错误:
TypeError: undefined is not an object (evaluating 'service[props.site].color')
但是当我尝试使用 service.facebook.name 访问时没有错误。
【问题讨论】:
标签: arrays json react-native assets react-props