2017-05-08 99 views
0

下面是部分在哪里发生问题的代码:不能得到阵营路由器的4环节的工作

import React, { Component } from 'react'; 
    import { connect } from 'react-redux'; 
    import { Redirect, Link, withRouter } from 'react-router-dom' 
    import * as actions from 'actions'; 

    class DashBoard extends Component { 
     constructor(props) { 
      super(props); 
     } 
     componentDidMount() { 
      this.props.dispatch(actions.deleteItems()); 
      this.props.dispatch(actions.fetchItems()); 
     } 
     render() { 
      let { items } = this.props; 
      let key = 0; 
      let renderItems =() => { 
       if (!items) { 
        return 
       } 
       return items.map((item) => { 
        let { action, author } = item.logs[item.logs.length - 1]; 
        return (
         <div className="dashboard-item"> 
          <h3>{item.name}</h3> 
          <div className="info-container"> 
           <span>Amount: {item.number}</span> 
           <span>{item.state}</span> 
          </div> 
          <span className="created">{`${action} by ${author}`}</span> 
          <span className="last-log">{`Last log: ${item.logs[0].action} by ${item.logs[0].author}`}</span> 
          <div className="buttons"> 
           <Link to='/'>Edit</Link> 
           <Link to={`/items/${item.id}/edit`}>Delete</Link> 
          </div> 
         </div> 
        ); 
       }) 
      } 

      if (this.props.auth.token) { 
       return (
        <div className="dashboard-container"> 
         {renderItems()} 
        </div> 
       ); 
      } else { 
       this.props.dispatch(actions.setError('You must log in.')) 
       return <Redirect to='/' /> 
      } 
     } 
    } 

    export default withRouter(connect(
     (state) => { 
      return state; 
     } 

)(DashBoard)); 

我得到重定向工作,但点击链接只是改变的URL在浏览器,其实我还是看到我的仪表板组件。如果我输入localhost:3000/items/随机ID /编辑我得到正确的结果。创建和点击一个链接什么也不做。网址栏除外,没有任何更改。用路由器破解似乎不适合我。然而,直接输入url的作品。我怎样才能解决这个问题?

编辑:路由定义

import React, { Component } from 'react'; 
import { Route, Link } from 'react-router-dom'; 
import { connect } from 'react-redux'; 
import Home from 'Home'; 
import Dashboard from 'Dashboard'; 
import EditItemForm from 'EditItemForm'; 
import NewItemForm from 'NewItemForm'; 

export class Main extends Component { 
    constructor(props) { 
     super(props); 
    } 
    render() { 
     let { auth, error } = this.props; 
     let renderError =() => { 
      if (error) { 
       return (
        <div className="error"> 
         <p>{error}</p> 
        </div> 
       ) 
      } else { 
       return (<div></div>) 
      } 
     } 
     return (
      <div> 
       {renderError()} 
       <Route exact={true} path="/" component={Home} /> 
       <Route path="/dashboard" component={Dashboard} /> 
       <Route path="/items/:id/edit" component={EditItemForm} /> 
       <Route path="/items/new" component={NewItemForm}/> 
      </div> 
     ); 
    } 
} 

export default connect(
    (state) => { 
     return state; 
    } 
)(Main); 

编辑#2:想通了,点击一个链接,即使并没有改变路径的路径>定位>路径,如果考虑作出反应DevTools

+0

您是否检查是否有不止一条路线与URL匹配?如果你来自React-Router v3.x,你必须改变url的匹配方式。 –

+0

你使用BrowserRouter还是路由器? – Syberic

+0

@Syberic,我编辑我的职务,并添加所有路由都定义 –

回答

0

所以我只是回滚到v3。谢谢你们的帮助!

1

我认为你可能需要在一个组件的所有路由上使用一个开关,因为即使我之前也有过类似的问题。然而这在反应路由器v3中起作用。希望这有助于ü:)欢呼

React Router 4

React Router v4 Unofficial Migration Guide

+0

在新版本的React路由器没有匹配 –

+0

@RomanSarder谢谢你,我只是错过了第4版的标签。 –

+0

@RomanSarder请让我们知道如果你的问题得到解决:) –

1

在我看来,有什么错相对于使用的代码阵营,你这里显示路由器。

我建议您简化应用程序,您发布主路由器组件,因为你看到你的仪表盘应该是罚款,但它可以帮助。

尝试做一个假字问好路线,做您的信息中心过渡到有..

通常,你可以在这里最有可能的是,阻止路由的更新嵌套的组件。通常在每个连接的组件中放置路由器都可以工作。或者,您可以尝试在与路由嵌套的每个组件中添加路由器。

我觉得应该有一个优雅为什么要解决这个问题,但也许这可以帮助你知道这个问题的来源。

然后尝试检查您的第三方库是否支持RR4。 祝你好运!