如何将导航道具传递给演示组件

问题描述 投票:0回答:2

对于Flatlist,我创建了一个演示组件并将项目作为道具传递给子组件,该组件工作正常。现在,我在演示组件中使用TouchableOpacity,在其他组件中使用navigate。我该如何实现??

MyFlastList:

_renderItem({item, index}) {
        return(
            <View>
              {index == 0 && <MachineInformationCard {...item}/>}
              {index > 0 && <MachineFunctionality {...item} />}   
            </View>
        )
    }

MyPresentational层:

import React from 'react';
import {View,Text, StyleSheet, TouchableOpacity} from 'react-native';
export default MachineFunctionality = (props) => {
    let acheivedPercentage = ((props.acheived_value/props.target_value)*100);
    let acheivedPercentageValue = acheivedPercentage;
    acheivedPercentage = acheivedPercentageValue + '%';
    return(
        <TouchableOpacity style={styles.CardItem} 
        onPress={()=> this.props.navigation.navigate('MachineFunctionalityDetail')}>
            <Text style={{fontSize:18, color:'black'}}>{props.level}</Text>
            <Text style={{marginTop: 5}}>{props.taget_label}</Text>
            <View style={[styles.barContainer, {width:'100%'}]}>
                {/* <Text style={{textAlign:'center', fontSize:12, padding:2}}>100%</Text> */}
            </View>
            <Text style={{marginTop: 5}}>{props.acheived_label} ({Math.ceil(acheivedPercentageValue)}%)</Text>
            <View style={[styles.barContainer,{width: acheivedPercentage}]}>
            </View>
        </TouchableOpacity>
    )
}
const styles = StyleSheet.create({
    CardItem: {
        flex: 1,
        height:120,
        padding:8,
        backgroundColor:'#e7e7e7',
        borderRadius: 5,
        overflow: 'hidden',
        margin: 5,
        shadowColor: 'black',
        shadowOpacity: 0.8,
        elevation: 8
    },
    barText: {
        justifyContent: 'center',
        fontSize: 13,
        margin: 10,
        fontWeight: 'bold',
    },
    barContainer: {
        height: 10,
        marginTop:5,
        backgroundColor:'lightskyblue',
        borderColor: 'blue',
        borderWidth: 1,
        borderRadius: 3,
        shadowOpacity: 0,
        elevation: 2,
    },

})

MyNavigator

const MachineStackNavigator = createStackNavigator({
  MachineHome: {screen: AllMachine, navigationOptions:{header: null}},
  MachineFunctionalityDetail: {screen:MachineFunctionalityDetail, navigationOptions:{header: null}},
  MachineProfile:{screen:MachineProfile, navigationOptions:{header: null}}
})
reactjs react-native react-navigation
2个回答
1
投票

我已经创建了一个用于处理导航问题的库,并且还阻止了this.props.navigation道具的使用。

React Navigation Helpers

用法非常简单。在您的App.js或导航器上,只需设置全局级别的导航器即可。

import NavigationService from "react-navigation-helpers";


<MainNavigator
  ref={navigatorRef =>
    NavigationService.setGlobalLevelNavigator(navigatorRef)
  }
/>

然后您通过以下简单代码段在项目上使用ANYWHERE:

NavigationService.navigate("home")

当然,您仍然需要导入NavigationService:)

您可以在图书馆的自述文件中找到更详细的文档。如果您还有问题,请随时提出,我们将很乐意为您提供帮助。


0
投票

首先只使用React Navigation提供的HOC,例如withNavigation,它会注入导航道具。

只需从演示组件的react-navigation中导入withNavigation

import {withNavigation} from 'react-navigation'; // Add this line

并像这样导出演示文稿组件:

export default withNavigation(MachineFunctionality);

这样,您无需更改组件中的其他任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.