简体   繁体   English

如何在导航到屏幕之前使用密码身份验证并使用 React Native 中的底部选项卡导航器对其进行调整?

[英]How to use password authentication before navigate to screens and adapt it with the bottom tab navigator in React Native?

This question is an extension of this question and it is from the same member who asked this.这个问题是这个问题的延伸,它来自提出这个问题的同一个成员。

First, his problem was, how to authenticate before navigating to screens.首先,他的问题是,如何在导航到屏幕之前进行身份验证。 But then, he asked, how to adapt that navigated new screen to the particular tab in the bottom tab navigator (Let's say Tab1).但随后,他问,如何使导航后的新屏幕适应底部选项卡导航器中的特定选项卡(比如说 Tab1)。 That means, after it is navigated to a particular screen after authenticated, he wants to click on another tab (Let's say Tab2), and then click on the previous tab (Tab1) and the navigated screen should still display on that previous tab (Tab1).这意味着,在经过身份验证后导航到特定屏幕后,他想单击另一个选项卡(比如说 Tab2),然后单击上一个选项卡(Tab1),导航屏幕仍应显示在前一个选项卡(Tab1 )。

I've provided my answer to this new question below...我已经在下面提供了对这个新问题的答案...

This is the solution that I'm suggesting.这是我建议的解决方案。

This answer is an extension of the answer in the first question .这个答案是第一个问题答案的延伸。

Home.js

import React, { Component } from 'react';
import { Text, View, TouchableOpacity, ScrollView } from 'react-native';
import PasswordInputModal from './PasswordInputModal'
import HelderScreen from 'path/to/HelderScreen';
import LolsScreen from 'path/to/LolsScreen';
import AthleanScreen from 'path/to/AthleanScreen';

export default class HomeScreen extends Component {
  constructor(props) {
    super(props); 
    this.state = {
      screen: null,
    }
  }

  switchScreen = () => {
    switch (this.state.screen) {
      case 'Helder' : return <HelderScreen />;
      case 'Lols'   : return <LolsScreen />;
      case 'Athlean': return <AthleanScreen />;
      default       : this.setState({ screen: null });
    }
  }

  render() {
    if(this.state.screen) { return this.switchScreen() }

    return (
      <View style={styles.container}>
        <ScrollView style={styles.flatlist}>
          <View style={styles.flatlist1}>
            <TouchableOpacity onPress={() => this.PasswordInputModal.open('Helder')}>
              <Text style={styles.item}>Empresa do Helder</Text>
            </TouchableOpacity>
          </View>
          <View style={styles.flatlist1}>
            <TouchableOpacity onPress={() => this.PasswordInputModal.open('Lols')}>
              <Text style={styles.item}>Lols Inc</Text>
            </TouchableOpacity>
          </View>
          <View style={styles.flatlist1}>
            <TouchableOpacity onPress={() => this.PasswordInputModal.open('Athlean')}>
              <Text style={styles.item}>Tesla Portugal</Text>
            </TouchableOpacity>
          </View>          
        </ScrollView>

        <PasswordInputModal
          ref={modal => this.PasswordInputModal = modal} 
          navigation={this.props.navigation}
          onAuthentication={(screen) => this.setState({ screen })} />
      </View>
    );
  }
}

Here, if the state called screen is set to the particular name of the screen, it will conditionally render that particular screen.在这里,如果调用screenstate设置为屏幕的特定名称,它将有条件地渲染该特定屏幕。 Otherwise, it will render the buttons to go to those screens.否则,它将向这些屏幕呈现 go 的按钮。

PasswordInputModal.js

import React, { Component } from 'react';
import { View, TextInput, Button } from 'react-native';
import Modal from 'react-native-modal';

export default class PasswordInputModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      password  : '',
      isVisible : false,
      screen    : null,
    };
  }

  open = (screen) => this.setState({ isVisible: true, screen: screen });

  close = () => this.setState({ isVisible: false });

  onPressSubmitButton = () => {
    //Use any authentication method you want according to your requirement.
    //Here, it is taken as authenticated if and only if the input password is "12345".

    const isAuthenticated = ("12345" == this.state.password); //If input password is '12345', isAuthenticated gets true boolean value and false otherwise.

    if(isAuthenticated) {
      this.props.onAuthentication(this.state.screen);
    }
    else {
      console.log("Invalid password"); //Prompt an error alert or whatever you prefer.
    }

    this.close();
  }

  renderModalContent = () => (
    <View>
      <TextInput type={'password'} value={this.state.password} onChangeText={(password) => this.setState({ password })} />
      <Button onPress={() => this.onPressSubmitButton()} title="Submit" color="#841584" />
    </View>
  );


  render() {
    return (
      <Modal
        isVisible={this.state.isVisible}
        backdropColor="#000000"
        backdropOpacity={0.9}
        animationIn="zoomInDown"
        animationOut="zoomOutUp"
        animationInTiming={600}
        animationOutTiming={600}
        backdropTransitionInTiming={600}
        backdropTransitionOutTiming={600}
      >
        {this.renderModalContent()}
      </Modal>
    );
  }
}

What I've done here is, when the user is authenticated, a state called screen is set to the name of the screen which should be displayed.我在这里所做的是,当用户通过身份验证时,一个名为screenstate被设置为应该显示的屏幕的名称。 Actually, this is not something like navigating.实际上,这不像导航。 This is actually called Conditional Rendering.这实际上称为条件渲染。

I didn't test this code myself.我自己没有测试这段代码。 I hope this will help to the member who asked this question.我希望这对提出这个问题的成员有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何导航到不在选项卡导航器中的本机反应页面? - How to navigate to a page in react native that is not in the tab navigator? 如何在底部选项卡导航器 react-navigation 中卸载非活动屏幕? - How to unmount inactive screens in bottom tab navigator react-navigation? React Native - 在没有 React Navigator 的情况下在屏幕之间导航 - React Native - Navigate between screens without React Navigator 在 React native 中创建自定义底部选项卡导航器 - Create custom bottom tab navigator in React native 在本机选项卡导航器的屏幕之间传递对象 - Passing objects between react native tab navigator's screens 如何在反应导航 5 中的选项卡导航器中的选项卡屏幕之间移动? - How to move between tab screens in tab navigator in react navigation 5? 刷新控件不适用于本机反应中的底部选项卡导航器 - Refresh Control not working with bottom tab navigator in react native 在 React Native 中从底部选项卡导航器的标题导航 - Navigation from the header of the bottom tab navigator in react native 在反应原生的底部选项卡导航器中更改屏幕时如何更改图标和文本的颜色? - How to change color of icon as well as text when changing screen in bottom tab navigator in react native? 如何在同一导航器中有条件地更改不同反应本机屏幕的 screenOptions? - How to conditionally change screenOptions for different react native screens in the same Navigator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM