【问题标题】:Can't navigate using react-navigation无法使用 react-navigation 进行导航
【发布时间】:2017-04-24 14:29:23
【问题描述】:

我正在使用 react-navigation 来导航我的 android 应用,我使用 react-navigation 和 redux。

App.js:

const AppRouteConfigs = {
    Home: {
        screen: HomeView
    },
    Completed: {
        screen: CompletedView
    }
}
export const AppNavigator = TabNavigator(AppRouteConfigs,{
    tabBarPosition: 'bottom',
    tabBarOptions: {
        showIcon: true
    }
});

@connect(state=>({
    nav: state.nav
}))
class App extends Component {
    componentDidMount(){
        SplashScreen.hide();
    }
    render(){
        return (
            <AppNavigator navigation={addNavigationHelpers({
                dispatch: this.props.dispatch,
                state: this.props.nav
            })}/>
        )
    }
}
export default App;

减速器:

const initialNavState = {
    index: 1,
    routes: [

        {key: 'InitB',routeName: 'Completed'},
        {key: 'InitA',routeName: 'Home'}
    ]
}

export default function(state=initialNavState,action){
    switch(action.type) {
        case 'Login':
            return AppNavigator.router.getStateForAction(NavigationActions.back(),state);
        case 'Logout':
            return AppNavigator.router.getStateForAction(NavigationActions.navigate({routeName: 'Completed'}),state)
        default:
            return state;
    }
}

主页视图:

@connect(state=>({
    todos: state.todos
}))
export default class HomeView extends Component{
    constructor(props){
        super(props);
    }
    static navigationOptions = {
        tabBarLabel: 'Home View',
        tabBarIcon: ({tintColor})=>(
            <Icon name="rocket" size={15} color="#900" />
        )
    }
    handleClick = ()=>{
        this.props.dispatch(NavigationActions.navigate({routeName: 'Completed'}))
    }
    render(){
        return (
            <View>
                <Text>Hey,I'm home page</Text>
                <Button onPress={this.handleClick} title="go to completed"/>
                <Icon name="rocket" size={30} color="#900" />
            </View>
        )
    }
}

在主页视图中,我想导航到已完成视图,

this.props.dispatch(NavigationActions.navigate({routeName: 'Completed'})) 

不起作用,我不知道为什么。

this.props.navigation.dispatch(NavigationActions.navigate({routeName: 'Completed'}))

也不行。

【问题讨论】:

    标签: reactjs react-native react-navigation


    【解决方案1】:

    试试这个:

    render() {
        const { navigate } = this.props.navigation
        return (
            <View>
                <Button
                    onPress={() => navigation.navigate('Completed')}
                    title='Completed'
                />
            </View>
        )
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2018-12-01
      相关资源
      最近更新 更多