繁体   English   中英

React Router在同一页面上呈现两个组件

[英]React Router renders two components on the same page

我已经看过类似的其他问题,但似乎没有一个能解决这个问题。

我想使用react路由器链接到另一个页面。

这是我的组件的返回:

          <NativeRouter>
            <View style={styles.container}>
                <TextInput
                    onChangeText={value => this.onChangeText('username', value)}
                    style={styles.input}
                    placeholder='username'
                />
                <TextInput
                    onChangeText={value => this.onChangeText('password', value)}
                    style={styles.input}
                    secureTextEntry={true}
                    placeholder='password'
                />
                <Button style={styles.button} title="Sign In" onPress={this.signIn.bind(this)} />
                <TextInput
                    onChangeText={value => this.onChangeText('confirmationCode', value)}
                    style={styles.input}
                    placeholder='Confirmation Code'
                />
                <Button style={styles.button} title="Confirm Sign In" onPress={this.confirmSignIn.bind(this)} />

                <Link to='/forgotPassword'>
                    <Text>Forgot Password</Text>
                </Link>
                <Switch>
                    <Route path='/forgotPassword' component={ForgotPassword} />
                </Switch>
            </View>
          </NativeRouter>

每当我点击链接时,该链接都会在同一页面上呈现ForgotPassword组件。

想法?

这是在我的app.js.里面。

        <Switch>
          <Route path="/signin" component={SignIn} />
          <Route path="/forgotPassword" component={ForgotPassword} />
        </Switch>
        </View>

url匹配时,组件中的Route将被ForgoPassword组件替换,您应该在父组件中定义路由

import React from "react";
import { StyleSheet, Text, View, AppRegistry } from "react-native";
import { NativeRouter, Route, Link } from "react-router-native";

const Home = () => <Text style={styles.header}>Home</Text>;
const SignIn = () => <Text style={styles.header}>SignIn</Text>;
const ForgotPassword = () => <Text style={styles.header}>ForgotPassword</Text>;

const App = () => (
  <NativeRouter>
    <View style={styles.container}>
      <View style={styles.nav}>
        <Link to="/" underlayColor="#f0f4f7" style={styles.navItem}>
          <Text>Home</Text>
        </Link>
        <Link to="/signIn" underlayColor="#f0f4f7" style={styles.navItem}>
          <Text>Sign In</Text>
        </Link>
        <Link
          to="/forgotPassword"
          underlayColor="#f0f4f7"
          style={styles.navItem}
        >
          <Text>Forgot Password</Text>
        </Link>
      </View>

      <Route exact path="/" component={Home} />
      <Route path="/signIn" component={SignIn} />
      <Route path="/forgotPassword" component={ForgotPassword} />
    </View>
  </NativeRouter>
);

const styles = StyleSheet.create({
  container: {
    marginTop: 25,
    padding: 10
  },
  header: {
    fontSize: 20
  },
  nav: {
    flexDirection: "row",
    justifyContent: "space-around"
  },
  navItem: {
    flex: 1,
    alignItems: "center",
    padding: 10
  },
  subNavItem: {
    padding: 5
  },
  topic: {
    textAlign: "center",
    fontSize: 15
  }
});

AppRegistry.registerComponent("MyApp", () => App);

砂箱

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM