2017-06-20 40 views
1

我正尝试通过组件文件中的条件语句重定向到其他组件。React JS组件中的重定向问题

的片段myfile.js

import { Redirect } from 'react-router-dom'; 
render(){ 
     if (isLoggedIn()) { 
      return(
       <Redirect to='/Myaccount' /> 
      ); 
     } 
    } 

当条件为真,它改变了浏览器的URL,但是内容不加载。
请让我知道我做错了什么。

+0

检查这个答案,我想这会回答你的问题https://stackoverflow.com/questions/44434041/nesting-routes-and-dynamically-routing-in-react-router-v4/44434648#44434648 –

+0

相同,重定向...... url在浏览器中改变,但页面内容不加载.... – Atul

+0

你使用withRouter,你也有任何错误或你有匹配的路线 –

回答

0

您可能会更好使用history.push类似以下内容。

if (isLoggedIn()) { 
    return(
     this.props.history.push("/Myaccount"); 
    ); 
} 
+0

虽然您不会返回,但您也不会在渲染函数中使用它 – Chris

+0

@Drum> this.props.history.push(“/ Myaccount”); >>>我试过,但也没有工作.... – Atul

+0

@Chris,我应该在哪里放置这个重定向条件.......? – Atul