如何在 React Native 中实现标签导航?

How to implement tab navigation in react native?

我正在用 React Native 创建一个新应用程序。在登录屏幕中,我正在尝试实现选项卡 navigation.I 想要在 Login formregister form 之间导航。实际上,我不想导航到另一个 page.I'我试图在这两个 forms 之间导航(就像在 coursera 应用程序中一样)。那可能吗?我不知道如何实现这个。

以下是react中的代码

<Container>
    <View style={styles.container}>
        <View style={styles.content}>
            <InputGroup style={styles.input}>
                <Input
                    label="Email"
                    placeholder="email@gmail.com"   />
                <Image source={require('../Images/envelope.png')} style={{width:30, height:30, marginRight:5}} />
            </InputGroup>
            <InputGroup style={styles.input}>

                <Input
                    label="Password"
                    placeholder="Password"
                    secureTextEntry />
                <Image source={require('../Images/lock.png')} style={{width:30, height:30, marginRight:5}} />

            </InputGroup>
            {this.state.isLoading ? ( <Spinner size="small" color="#000000" />
            ) : (
                <Button style={styles.button} onPress={() => this.onPressLogin()} >
                    <Text style={{paddingLeft:50}}>Login</Text>
                </Button>
            )}

            <Tabs selected={this.state.page} style={{backgroundColor:'white'}}
                  selectedStyle={{color:'red'}} onSelect={el=>this.setState({page:el.props.name})}>

                <Text name="first" selectedIconStyle={{borderTopColor: 'red',borderTopWidth:2}}>Login</Text>
                <Text name="second" selectedIconStyle={{borderTopWidth:2,borderTopColor:'red'}}>Register</Text>
            </Tabs>
        </View>

    </View>
</Container>

主界面截图如下

这是你想要的吗?

如果是这样,实施起来并不难。

首先,安装react-navigation模块:

npm install --save react-navigation

其次,您提到您不想制作两个屏幕。然而,在这种情况下,它们实际上仍然是 2 个独立的屏幕。让我们继续制作两个屏幕:LoginScreenRegisterScreen 像这样:

// This is LoginScreen
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { Icon } from 'react-native-elements';

class LoginScreen extends Component {
    // This is for the tab bar configuration
    static navigationOptions = {
        title: 'Login',
        header: null,
        tabBarIcon: ({ tintColor }) => {
            return (<Icon name='input' size={30} color={tintColor} />)
        }
    };

    render() {
        return (
            <View style={{ ... }}>
                <Text style={{ ... }}>
                    Login Screen
                </Text>
            </View>
        );
    }
}

export default LoginScreen;

// This is RegisterScreen
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { Icon } from 'react-native-elements';

class RegisterScreen extends Component {
    static navigationOptions = {
        title: 'Register',
        header: null,
        tabBarIcon: ({ tintColor }) => {
            return (<Icon name='person-add' size={30} color={tintColor} />)
        }
    };

    render() {
        return (
            <View style={{ ... }}>
                <Text style={{ ... }}>
                    Register Screen
                </Text>
            </View>
        );
    }
}

export default RegisterScreen;

然后,在您的 App.js 文件中,执行以下操作:

import React, { Component } from 'react';
import { TabNavigator } from 'react-navigation';

import LoginScreen from 'path/to/LoginScreen';
import RegisterScreen from 'path/to/RegisterScreen';

class App extends Component {
    render() {
        const MainNavigator = TabNavigator({
            login: { screen: LoginScreen },
            register: { screen: RegisterScreen }
        });

        return (
            <MainNavigator />
        );
    }
}

export default App;

简单的流程代码!

import React from 'react';
import { Text, View } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createBottomTabNavigator } from 'react-navigation-tabs';

class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>Home!</Text>
      </View>
    );
  }
}

class SettingsScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>Settings!</Text>
      </View>
    );
  }
}

const TabNavigator = createBottomTabNavigator({
  Home: HomeScreen,
  Settings: SettingsScreen,
});

const AppContainer = createAppContainer(TabNavigator);

// without the style you will see a blank screen
export default () => (
  <View style={{ flex: 1 }}>
    <AppContainer />
  </View>
);