【问题标题】:Can't make React-Router render two different components, always render root不能让 React-Router 渲染两个不同的组件,总是渲染根
【发布时间】:2017-04-17 10:50:17
【问题描述】:

我有这两个完全相互独立的组件。我想在输入/ 时渲染App 并在我转到/#/about 时渲染About

我得到了这段代码(但我测试了很多其他代码):

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, Link, browserHistory } from 'react-router'
import App from './App';
import About from './About';

ReactDOM.render(
  <Router history={browserHistory}>
    <Route path="/" component={App} >
      <Route path="/about" component={About} />
    </Route>
  </Router>
  , document.getElementById('root')
);

我也尝试过类似的东西

<Route path="/about" component={About} />
<Route path="/" component={App} />

并将 /about 更改为 /#/about, about

但它总是呈现“后备”/,无论如何它总是走这条路线。

如何让这个应用程序正确导航到//about 并渲染AppAbout 组件?

@edit

假设我的 About 组件已损坏,我删除了第一个 Route 并仅保留了 /about(仅保留了 /about 路由):

<Route path="/about" component={App} />

(我在之前的测试中也尝试保留 About)并将 /about 更改为 about/#/about

我在控制台上收到此错误:

“VM3651 bundle.js:30801 警告:[react-router] 位置“/#/about”不匹配任何路由”

@edit 2

我按照@Dominic 发布的示例进行了更改。我必须进行一些修改以确保两个组件都能呈现。我将 {this.props.children} 添加到所有组件以了解会发生什么。

//imports
ReactDOM.render(
<Router history={browserHistory}>
  <Route path="/" component={About} >
    <IndexRoute component={App} />
    <Route path="/about" component={Other} />
  </Route>
</Router>
,document.getElementById('root'));

路由http://localhost:3000/#/about正在渲染:

> 关于 > 应用程序

所以它正在渲染 IndexRoute,它没有被/about 捕获。

这正是我现在所需要的,因为我不想要根组件,我想要 2 条路由到 2 个不同且独立的组件。我需要类似两条兄弟路线的东西。

@edit

About.js

import React, { Component } from 'react';

class About extends Component {
  render() {
    return (
      <div>
        About page
        {this.props.children}
      </div>
    );
  }
}

export default About;

解决方案:

由于我在 URL 中使用 HASH (#),我应该在 &lt;Router history={hashHistory}&gt; 中使用来自 React Router 的 hashHistory

【问题讨论】:

  • 我注意到这里与github.com/ReactTraining/react-router 的唯一区别是他们使用了 render(( .....), document.getElement....);当您只使用 1 () 时,我不敢相信这就是原因,但谁知道呢
  • 你能展示你的关于组件吗?
  • 你如何重定向到关于页面? ?还是手动输入网址?您正在使用 browserHistory,因此 url 中不应存在哈希 :)
  • 我在 url 中使用 #(哈希)(参见新编辑)。所以我应该去http://localhost:3000/about? webpack 服务器响应“Cannot GET /about”
  • hashHistory 一起工作!谢谢

标签: javascript reactjs routes react-router react-jsx


【解决方案1】:

这些路线在我看来是正确的。您是否在控制台中收到任何错误?也许您的 About 组件未定义,因此无法呈现。你能发布你的关于组件吗?

【讨论】:

  • 假设我的 About 组件已损坏,我删除了第一个 Route 并仅保留了 /about: &lt;Route path="/about" component={App} /&gt; (我在之前的测试中也尝试保留 About)并更改了 @ 987654323@ 到 about/#/about。我在控制台上收到此错误:“VM3651 bundle.js:30801 警告:[react-router] 位置“/#/about”不匹配任何路由”
  • 就像一个测试把你的 About 组件放在你的 "/" 路由上
  • 并且仅供参考“/about”是正确的路线
  • 你还能在这里发帖吗?
【解决方案2】:

您对路由的工作方式感到困惑 - AboutApp 路由的子节点,因此为了渲染 About,它必须渲染 App

换句话说,您的App 组件是“外壳”,它下面的所有组件都会在它内部呈现(通过props.children)。

你应该添加另一个路由来渲染/

import { ..., IndexRoute } from 'react-router'

<Route path="/" component={App} >
  <IndexRoute component={Home} />
  <Route path="about" component={About} />
</Route>

您的App 不包含特定于路线的内容,它会更像这样:

<div id="app">
  <nav>app navigation</nav>
  <main class="route-content">{props.children}</main>
</div>

文档:https://github.com/ReactTraining/react-router/blob/master/docs/guides/RouteConfiguration.md#adding-an-index

【讨论】:

  • 但是在该页面的顶部,他们显示了应用程序的第一个版本,没有索引路由,并且它说应用程序已经知道如何呈现 / 和 /about ,没有 indexRoute . imgur.com/a/yOKuh
  • 如果您查看该图像/呈现 App,而其他人呈现 App 和预期的组件
猜你喜欢
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 2016-11-05
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多