reactjs - 从 0.13 升级到 2.8.1 后 React-router 不工作

标签 reactjs react-router

我将我的 react-router 从版本 0.13 升级到 2.8.1,然后它给出了一个错误,指出“无法加载资源:服务器响应状态为 404(未找到)”。

这是我的代码。

import React from 'react'
import Router from 'react-router'

import App from './components/app';
import Main from './components/main';
import CreateOrganization from './components/create-organization';
import ManageUsers from './components/manage-users';
import FldList from './components/fld-list';
import FldView from './components/fld-view';
import WeighIn from './components/weigh-in';
import MatchFlds from './components/match-flds';
import NotFound from './components/not-found';

var { Route, DefaultRoute, NotFoundRoute } = Router;

var routes = (
    <Route handler={App}>
        <DefaultRoute handler={Main} />
        <Route name="CreateOrganization" path="create-organization" handler={CreateOrganization}></Route>
        <Route name="manageUsers" path="manage-users" handler={ManageUsers}></Route>
        <Route name="fldList" path="fld-list" handler={FldList}></Route>
        <Route name="fldView" path="fld-view/:fldId" handler={FldView}></Route>
        <Route name="weighIn" path="weigh-in" handler={WeighIn}></Route>
        <Route name="matchFlds" path="match-flds" handler={MatchFlds}></Route>
        <NotFoundRoute handler={NotFound} />
    </Route>
);

Router.run(routes, function (Handler) {
    React.render(<Handler />, document.getElementById('react-container'));
});

我应该怎么做才能解决这个错误?

最佳答案

此答案已在 React Router ver 2.7.0 上测试

React Router 的第 2 版引入了 IndexRoutebrowserHistory 的概念。

关于如何配置路由的粗略概述如下。请查看官方文档以根据您的用例进行调整。

import { Router, Route, IndexRoute, browserHistory } from 'react-router';

var routes = (
    <Router history={browserHistory}>
      <Route path="/" component={App} />
        <IndexRoute component={Main} />
        <Route path="create-organization" component={CreateOrganization} />
        <Route path="manage-users" component={ManageUsers} />
        <Route path="fld-list" component={FldList} />
        <Route path="fld-view/:fldId" component={FldView} />
        <Route path="weigh-in" component={WeighIn} />
        <Route path="match-flds" component={MatchFlds} />
        <Route path="*" component={NotFound} />
      </Route>
    </Router>
);

React.render(
  routes,
  document.getElementById('react-container')
);

// Also, in newer versions of React you can use ReactDOM for mounting your app.
// import ReactDOM from 'react-dom'
//ReactDOM.render(
//  routes,
//  document.getElementById('react-container')
//);

关于reactjs - 从 0.13 升级到 2.8.1 后 React-router 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39762495/

相关文章:

javascript - 如何使用react-flip-move为表格制作动画

javascript - react : How does React make sure that useEffect is called after the browser has had a chance to paint?

javascript - 为 ReactJS 中缺少的属性设置默认值

javascript - React Router 不会在登录和注销时重定向

reactjs - 如何在 react 中的标签路由器中没有标签的情况下进行路由?

node.js - React 中的不同页面服务是如何工作的?

javascript - React 组件在 React bootstrap Navbar 下渲染

javascript - 无法读取未定义错误的属性 'username'

react-router - 如何在React Router v4中传递 Prop ?

javascript - React-Redux 购物车 - 减少/增加商品数量