ReactJS您如何在React中的页面之间切换? [英] ReactJS How do you switch between pages in React?

查看:204
本文介绍了ReactJS您如何在React中的页面之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,从Angular/AngularJS背景中获得状态,每个状态都是一个单独的页面.例如.在社交网络中,您可以将状态与您的供稿,包含好友列表的状态或可以查看个人资料的状态等联系在一起.非常简单.对我来说,React没那么多.

So coming from an Angular/AngularJS background, you have states, and each state is a separate page. E.g. in a social network you can have a state with your feed, a state with a list of your friends, or a state to see a profile etc. Pretty straightforward. Not so much in React for me.

因此,假设我有一个包含2页的应用程序:产品列表和单个产品视图.在它们之间进行切换的最佳方法是什么?

So let's say I have an application with 2 pages: a list of products and a single product view. What would be the best way to switch between them?

最简单的解决方案是拥有一个 object ,该对象具有一个 stateName 且是一个 boolean

The simplest solution is to have an object which has a stateName and is a boolean

constructor() {
  super();

  this.state = {
    productList: true,
    productView: false
  }
}

然后在 render()函数

return() {
  <div className="App">
    // Or you could use an if statement
    { this.state.productList && <ProductList /> }
    { this.state.productView && <ProductView product={this.state.product} /> }
  </div>
}

很简单,对吧?当然,对于2页的应用程序.

Pretty straightforward, right? Sure, for a 2 page application that is.

但是,如果您的大型应用具有 10 20 100 页面,该怎么办?渲染的 return()部分将是一团糟,并且跟踪每个状态的状态都是疯狂的.

But what if you have a large app with 10, 20, 100 pages? The return() part of the render would be a mess, and it would be madness to track the status of every state.

我认为应该有更好的方法来组织您的应用程序.我尝试使用Google搜索,但是找不到任何有用的东西(除了使用 if 条件运算符之外).

I believe there should be a better way to organize your app. I tried googling around but I couldn't find anything useful (except for using if else or conditional operators).

推荐答案

我建议您检查

I'd recommend you to check react-router to solve this situation

它很容易让您创建这样的自定义路线:

It easily allows you to create custom routes like this:

import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";

const BasicExample = () => (
  <Router>
    <div>
      <ul>
        <li>
          <Link to="/">Home</Link>
        </li>
        <li>
          <Link to="/about">About</Link>
        </li>
        <li>
          <Link to="/topics">Topics</Link>
        </li>
      </ul>

      <hr />

      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/topics" component={Topics} />
    </div>
  </Router>
);

const Home = () => (
  <div>
    <h2>Home</h2>
  </div>
);

const About = () => (
  <div>
    <h2>About</h2>
  </div>
);

const Topics = ({ match }) => (
  <div>
    <h2>Topics</h2>
    <ul>
      <li>
        <Link to={`${match.url}/rendering`}>Rendering with React</Link>
      </li>
      <li>
        <Link to={`${match.url}/components`}>Components</Link>
      </li>
      <li>
        <Link to={`${match.url}/props-v-state`}>Props v. State</Link>
      </li>
    </ul>

    <Route path={`${match.url}/:topicId`} component={Topic} />
    <Route
      exact
      path={match.url}
      render={() => <h3>Please select a topic.</h3>}
    />
  </div>
);

const Topic = ({ match }) => (
  <div>
    <h3>{match.params.topicId}</h3>
  </div>
);

export default BasicExample;

这篇关于ReactJS您如何在React中的页面之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆