使用-4\f25 React-Router-4 \f6如何以编程方式设置索引路由-4\f25 /
例如,如果用户未通过身份验证,则应触发:
<Route path="/" component={LandingPage}>如果用户已通过身份验证:
<PrivateRoute path="/dashboard" component={Dashboard} />有关PrivateRoute的信息,请参阅
更新的尝试
const WithMainLayout = ({component: Component, ...more}) => {
return <Route {...more} render={props => {
return (
<MainLayout {...props}>
<Component {...props} />
</MainLayout>
);
}}/>;
};
const isLoggedIn = () => {
console.log('do it')
return true;
};
....
<WithMainLayout exact path="/" component={Home} render={() => (
isLoggedIn() ? (
<Redirect to="/dashboard" />
) : (
<Home />
)
)}/>查看上面的尝试,由于某些原因,console.log不会在isLoggedIn函数中输出任何内容。
发布于 2017-06-26 23:44:30
所以在你的更新代码中,问题是你返回
<Route {...more} render={props => {
因此,实际上发生的是,传递给WithMainLayout组件的render属性被您的自定义属性所覆盖,因此永远不会调用isLoggedIn。
解决方案很简单,您只需交换{...more}和render={props => {}},还可以用WithMainLayout包装Home组件,这样它就不会错过布局
您的代码将如下所示
const WithMainLayout = ({component: Component, ...more}) => {
return <Route render={props => {
return (
<MainLayout {...props}>
<Component {...props} />
</MainLayout>
);
}} {...more} />;
};
const isLoggedIn = () => {
console.log('do it')
return true;
};
....
<WithMainLayout exact path="/" component={Home} render={() => (
isLoggedIn() ? (
<Redirect to="/dashboard" />
) : (
<WithMainLayout component={Home} />
)
)}/>发布于 2017-06-26 22:26:40
您可以使用history属性。你可以在这里阅读到:https://reacttraining.com/react-router/web/api/history
本质上,您将组件包装在withRouter HOC中,它将把history属性传递给您的组件,您将在此处看到:https://reacttraining.com/react-router/web/api/withRouter。它与React Recompose非常好地融合在一起。我修改了react路由器文档的“基本示例”,将其作为在history属性中使用withRouter的示例
// create-react-app test && cd test && npm i && npm install react-router-dom
// replace contents of App.js then `npm run start`
import React, { Component } from 'react';
import { withRouter } from 'react-router'
import {
BrowserRouter as Router,
Route,
Link
} from 'react-router-dom'
class BasicExample extends Component {
render() {
return(
<Router>
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
</ul>
<hr/>
<Route exact path="/" component={withRouter(Home)}/>
<Route path="/about" component={About}/>
</div>
</Router>
)
}
}
class Home extends Component {
render() {
const {history} = this.props;
const handleClick = (e) => {
history.push("/about")
}
console.log(history)
return (
<div>
<h2>Home</h2>
<button onClick={handleClick}>To about</button>
</div>
)
}
}
const About = () => (
<div>
<h2>About</h2>
</div>
)
export default BasicExample;如果要在JSX中创建链接,请使用Link组件,如下所示
<Link to="/">...</Link>如果您是在PrivateRoute组件中执行此操作,我认为您需要的是更像Redirect组件的东西:https://reacttraining.com/react-router/web/api/Redirect
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
props.isAuthorized ? (
<Component {...props}/>
) : (
<Redirect to={{
pathname: '/',
state: { from: props.location }
}}/>
)
)}/>)发布于 2017-06-26 22:29:30
我会将onEnter方法放在IndexRoute上,并根据需要以编程方式将用户发送到正确的位置:
<IndexRoute onEnter={handlePath}>其中处理程序如下所示,用适当的代码替换userIsAutheticaed:
function handlePath(route, replace) {
if (userIsAuthenticated) {
replace('/dashboard');
} else {
replace('/');
}
}https://stackoverflow.com/questions/44761854
复制相似问题