我正在做一个 react-native 项目,在那里我很难理解 props 在功能组件之间的工作方式。我的要求是创建一个可重用的按钮组件,我可以在我的项目内的资源文件中传递图像位置,因此它会为我创建按钮。出于某种原因,如果我手动提供所需的位置,它将起作用并为我创建按钮,但如果我\我将该位置作为我创建的道具传递,则由于某种原因它将不起作用。我的代码如下。
按钮组件
import React, { Component } from 'react';
import {
View,
StyleSheet,
Image,
TouchableOpacity
} from 'react-native';
const ButtonWithImage = (props) => {
const {buttonStyle} = styles;
const clickEvent = () => {}
return (
<TouchableOpacity onPress= {clickEvent}style={buttonStyle}>
<Image
source={props.imagePath}
style={styles.ImageIconStyle}
/>
</TouchableOpacity>
);
};
const styles = {
buttonStyle: {
//alignSelf:'stretch',
height: 50,
width:50,
paddingTop:0,
flexDirection: 'row'
}
};
export default ButtonWithImage;
我创建按钮并传递道具的地方
import React, { Component } from 'react';
import {
View,
StyleSheet,
Dimensions,
} from 'react-native';
import FooterIcons from './ButtonWithImage'
const Footer = () => {
return (
<View style={styles.footerStyle}>
<FooterIcons imagePath = {'./images/homeButton/homeBtn.png'} />
</View>
);
};
const styles = StyleSheet.create({
footerStyle: {
height: 60,
width: 100,
// justifyContent:'flex-start'
},
});
export default Footer;