【问题标题】:React native: passing image path as params through 2 componentsReact native:通过 2 个组件将图像路径作为参数传递
【发布时间】:2019-09-04 15:12:39
【问题描述】:

我正在尝试通过 react-navigation 参数将图像路径向下传递到一个组件,并从该组件作为道具传递到第三个组件。

这是一个聊天应用程序,组件的结构是:

ChatsMain.js -->(点击SingleChat,导航时传递参数)--> SingleConversation.js --> Message

我可以将“名称”作为参数传递,但不能传递图像的 uri,甚至不能将其分配给变量。

我在 SingleChat 上看到的名称可以毫无问题地传递给对应的对话,但是当我传递图像时出现错误: '失败的道具类型:无效的道具source提供给Image。'

我希望它足够清楚,我认为这不是正确的方法,但我是新手反应原生!

ChatsMain.js

const imageNames = {
    juf: require('../assets/juf.png'),
    meester: require('../assets/meester.png'),
    groep: require('../assets/groep.png'),
    onderwerpen: require('../assets/onderwerpen.png')
}

export default class ChatsMain extends React.Component {
    render() {
        return (

                        <SingleChat
                            image={imageNames.juf}
                            backgroundColor={yellow}
                            borderColor={blue}
                            name='Juf Elsa'
                            onPress={() => this.props.navigation.navigate('SingleConversation', {name: 'Juf Elsa', image: imageNames.juf})}
                        />
)

SingleConversation.js


export default class SingleConversation extends React.Component {

    static navigationOptions = ({ navigation }) => {
        return {
            title: navigation.getParam('name', 'Chat'),
            headerStyle: [titleBar, { backgroundColor: yellow }],
            headerTitleStyle: [title, { color: blue }]
        }
    }
    render() {
        return (
            <View style={chatMainContainer}>
                    <ScrollView style={chatsContent}>
                        <Message image={({navigation}) => navigation.getParam('image', 'image')} />
                    </ScrollView>
            </View>
        );
    }
}

消息.js

export default class ReceivedFirst extends React.Component {

    render(
        ){
        return(
            <View style={textWithPicture}>
            <View style={[chatPhotoContainer, { borderColor: yellow }]}>
                <Image source={this.props.image} style={{ width: 70, height: 70}} />
.....
.....
)

【问题讨论】:

    标签: javascript react-native react-navigation


    【解决方案1】:

    您可以使用this.props.navigation.state.params 代替this.props.navigation.getParam

    export default class SingleConversation extends React.Component {
     constructor(props) {
        super(props);
        this.state = {
          image: this.props.navigation.state.params.image
        };
      }
    ...
    Message image={this.state.image} />
    
    

    或 您需要从道具中获取导航。

        render() {
         const { navigation } = this.props
            return (
                <View style={chatMainContainer}>
                        <ScrollView style={chatsContent}>
                            <Message image={() => navigation.getParam('image', 'image')} />
                        </ScrollView>
                </View>
            );
        }
    

    【讨论】:

    • @AlbaFerrari 如果我的回答有帮助,请您选择“V”符号和箭头作为我的回答吗?
    • 我试过但收到以下消息:“感谢您的反馈!声望低于 15 人的投票将被记录,但不要更改公开显示的帖子得分。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 2021-11-09
    相关资源
    最近更新 更多