【问题标题】:React Nested Routes throwing error - Meteor反应嵌套路线抛出错误 - 流星
【发布时间】:2017-08-08 11:39:30
【问题描述】:

尝试设置一个简单的全局布局(App),并在 App 中渲染路径 /apply。 我不断收到警告:

您不应在同一路由中使用<Route component><Route children><Route children> 将被忽略

我可以说 100% 适用于这种情况,但 Google 没有为我的问题提供相关答案/解决方案。

Routes.jsx

import React from 'react';
import { render } from "react-dom";
import { Router, Route, IndexRoute } from "react-router";

import createBrowserHistory from "history/createBrowserHistory";
const history = createBrowserHistory();

// route components
import App from "./App.jsx";
import ApplyPage from "./pages/ApplyPage.jsx";

export const renderRoutes = () => (
  <Router history={history}>
      <Route path="/" component={App}>
        <Route path="apply" component={ApplyPage}/>
      </Route>
  </Router>
);

App.jsx

import React, { Component } from 'react'

import TopBar from "./components/TopBar.jsx"
import LeftMenuContainer from "./components/LeftMenuContainer.jsx"
import LivePurchases from "./components/LivePurchases.jsx"

export default class App extends Component {
    render() {
        console.log(this.props);
        return (
            <div className="App">
                <div className="flexWrapperGlobal">
                    <TopBar/>
                    <div className="contentContainer">
                        <LeftMenuContainer/>
                        <div className="bodyContainer">
                            <LivePurchases/>
                            <div className="siteContentContainer">
                                {this.props.children}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
}

ApplyPage.jsx

import React, { Component } from 'react'

export default class ApplyPage extends Component {
    render() {
        return (
        <div className="applyContainer">
            <div className="applySubContainer">
                <div className="applyBlock">
                    <h4>Seller Application</h4>
                    <form>
                        <h5>Roblox Account</h5>
                        <input type="text" placeholder="Username"/>
                        <h5>Selling Experience</h5>
                        <textarea type="text" placeholder="Selling Experience"/>
                        <h5>Extra Information</h5>
                        <textarea type="text" placeholder="Extra Information"/>
                    </form>
                    <a className="btn">Send</a>
                </div>
            </div>
        </div>
        )
    }
}

【问题讨论】:

标签: reactjs meteor react-router


【解决方案1】:

我今天遇到了同样的问题,在这里找到了解决方案:Nested Routes in v4

<App>
 <Route ....... />
 <Route ....... />
</App>

这对我有用。

【讨论】:

  • 你需要使用Route的'exact'属性来区分/parent和/parent/child
【解决方案2】:

找到答案,以防万一有人遇到相同的例子。

您现在将路由嵌套到渲染的顶级对象中,在我的例子中是 App

所以你必须将&lt;Route path="/apply" component={ApplyPage} /&gt; 移动到App.jsx,像这样:

render() {
    return (
        <div className="App">
            <div className="flexWrapperGlobal">
                <TopBar/>
                <div className="contentContainer">
                    <LeftMenuContainer/>
                    <div className="bodyContainer">
                        <LivePurchases/>
                        <div className="siteContentContainer">
                            <Route path="/apply" component={ApplyPage} />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    )
}

【讨论】:

  • 现在不用加&lt;Link&gt;了吗?它会只这样做吗?
猜你喜欢
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 2014-09-03
  • 2016-08-09
  • 2019-05-20
  • 2020-09-08
  • 1970-01-01
  • 2019-01-08
相关资源
最近更新 更多