【问题标题】:react router native renders linked component over previous one反应路由器本机将链接组件呈现在前一个之上
【发布时间】:2018-07-15 23:05:24
【问题描述】:

当我链接到一个组件时,它会呈现但在现有组件之上。 它不会“导航”,视图不会被链接的视图替换。

App.js:

import React, { Component } from 'react'
import {
  Text,
  TextInput,
  View,
  AppRegistry,
  TouchableHighlight,
  Image,    
  TouchableOpacity,
} from 'react-native'
import styles from './LoginStyleSheet';
import CircleCheckBox , {LABEL_POSITION} from 'react-native-circle-checkbox';
import SocialFooter from './SocialFooter';
import { NativeRouter, Route, Link, Redirect, withRouter } from 'react-router-native'
import TelUtiles from './TelUtiles';
//const { navigation } = this.props;
//const resp = navigation.getParam('resp', 'NO-resp');

const AuthExample = () => (

  <NativeRouter>
    <View style={styles.container}>
      <View style={styles.container_logo}>
        <Image source={require('./img/logo-main.png')} style={styles.logo_img} />
      </View>
      <View style={styles.welcomeMsg}>                                                         
        <Text 
            style={styles.welcomeMsg_textTop}>
            ¡Bienvenido a nuestra 
        </Text>
        <Text 
            style={styles.welcomeMsg_textBottom}>
            familia mascotera!
        </Text>
        <View style={styles.container_input_dni}>
            <TextInput placeholder='DNI:' placeholderTextColor="#E3A141" underlineColorAndroid="#E3A141" style={styles.input_dni} />                        
        </View>
        <Link
          to="/protected"
          style={styles.navItem_login}
          underlayColor='#f0f4f7'>
            <Text style={styles.navItem_login_text}>Protected Page</Text>
        </Link>
      </View>
      <AuthButton/>
      <View style={styles.nav}>
        <Link
          to="/public"
          style={styles.navItem}
          underlayColor='#f0f4f7'>
            <Text>Public Page</Text>
        </Link>
        <Link
          to="/protected"
          style={styles.navItem}
          underlayColor='#f0f4f7'>
            <Text>Protected Page</Text>
        </Link>
        <Link
            to="/TelUtiles"
            style={styles.navItem_login}
            underlayColor='#f0f4f7'>
            <Image source={require('./img/icono-tel.png')} style={{width:70, height:70,margin:10}} />
        </Link>
      </View>

      <Route path="/public" component={Public}/>
      <Route path="/login" component={Login}/>
      <Route path="/TelUtiles" component={TelUtiles}/>
      <PrivateRoute path="/protected" component={Protected}/>


    </View>
  </NativeRouter>


)

const fakeAuth = {
  isAuthenticated: false,
  authenticate(cb) {
    this.isAuthenticated = true
    setTimeout(cb, 100) // fake async
  },
  signout(cb) {
    this.isAuthenticated = false
    setTimeout(cb, 100)
  }
}

const AuthButton = withRouter(({ history }) => (
  fakeAuth.isAuthenticated ? (
    <View>
      <Text>Welcome!</Text>
      <TouchableHighlight style={styles.btn} underlayColor='#f0f4f7' onPress={() => {
        fakeAuth.signout(() => history.push('/'))
      }}><Text>Sign out</Text></TouchableHighlight>
    </View>
  ) : (
    <Text>You are not logged in.</Text>
  )
))

const PrivateRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={props => (
    fakeAuth.isAuthenticated ? (
      <Component {...props}/>
    ) : (
      <Redirect to={{
        pathname: '/login',
        state: { from: props.location }
      }}/>
    )
  )}/>
)

const Public = () => <Text style={styles.header}>Public</Text>
const Protected = () => <Text style={styles.header}>Protected</Text>

class Login extends Component {
  state = {
    redirectToReferrer: false
  }

  login = () => {
    fakeAuth.authenticate(() => {
      this.setState({ redirectToReferrer: true })
    })
  }

  render() {
    const { from } = this.props.location.state || { from: { pathname: '/' } }
    const { redirectToReferrer } = this.state

    if (redirectToReferrer) {
      return (
        <Redirect to={from}/>
      )
    }

    return (
      <View>
        <Text>You must log in to view the page at {from.pathname}</Text>

        <TouchableHighlight style={styles.btn} underlayColor='#f0f4f7' onPress={this.login}>
          <Text>Log in</Text>
        </TouchableHighlight>
      </View>
    )
  }
}

export default AuthExample

【问题讨论】:

  • 您是否在使用多个NativeRouter 组件?尝试在应用顶部仅使用一个 NativeRouter
  • 不,NativeRouter 就在父组件中
  • 能否将包含路由逻辑的整个组件包含在问题中?
  • 你去。谢谢。我不得不输入那个虚拟文本,因为它不允许我添加代码:S
  • 您是否尝试过将所有Route 组件包装在Switch 组件中?

标签: javascript reactjs react-native react-router


【解决方案1】:

如果您只想在某个路由上呈现 AuthExample 的内容,您可以将其分解为一个单独的组件并在索引路由 / 上呈现它,并使用 Switch 组件来确保一次只使用Switch 中的Route 组件之一。

示例

const Main = () => (
  <View>
    <View style={styles.welcomeMsg}>
      <Text style={styles.welcomeMsg_textTop}>¡Bienvenido a nuestra</Text>
      <Text style={styles.welcomeMsg_textBottom}>familia mascotera!</Text>
      <View style={styles.container_input_dni}>
        <TextInput
          placeholder="DNI:"
          placeholderTextColor="#E3A141"
          underlineColorAndroid="#E3A141"
          style={styles.input_dni}
        />
      </View>
      <Link
        to="/protected"
        style={styles.navItem_login}
        underlayColor="#f0f4f7"
      >
        <Text style={styles.navItem_login_text}>Protected Page</Text>
      </Link>
    </View>
    <AuthButton />
    <View style={styles.nav}>
      <Link to="/public" style={styles.navItem} underlayColor="#f0f4f7">
        <Text>Public Page</Text>
      </Link>
      <Link to="/protected" style={styles.navItem} underlayColor="#f0f4f7">
        <Text>Protected Page</Text>
      </Link>
      <Link
        to="/TelUtiles"
        style={styles.navItem_login}
        underlayColor="#f0f4f7"
      >
        <Image
          source={require("./img/icono-tel.png")}
          style={{ width: 70, height: 70, margin: 10 }}
        />
      </Link>
    </View>
  </View>
);

const AuthExample = () => (
  <NativeRouter>
    <View style={styles.container}>
      <View style={styles.container_logo}>
        <Image
          source={require("./img/logo-main.png")}
          style={styles.logo_img}
        />
      </View>

      <Switch>
        <Route exact path="/" component={Main} />
        <Route path="/public" component={Public} />
        <Route path="/login" component={Login} />
        <Route path="/TelUtiles" component={TelUtiles} />
        <PrivateRoute path="/protected" component={Protected} />
      </Switch>
    </View>
  </NativeRouter>
);

【讨论】:

  • 谢谢!!!。现在的问题是,当我从设备上点击后退箭头时,应用程序最小化并且没有返回到上一个路线,我是否错过了其他东西?
  • @Guille 如果您想使用后退按钮进行导航,您必须使用BackButton 组件作为NativeRouter 组件的第一个子组件。
  • 再次感谢!!,非常感谢您的帮助:D
  • 嗨@Tholle,我应该如何管理“返回”按钮?,我的意思是如何获取上一个访问的屏幕?谢谢!
猜你喜欢
  • 2018-04-15
  • 1970-01-01
  • 2019-02-21
  • 1970-01-01
  • 2019-03-04
  • 2019-04-28
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
相关资源
最近更新 更多