0
如何使用反應路由器4有條件地將路由推送到歷史記錄?我收到錯誤'history.push不是函數'。history.push in react router 4
我不在尋找重定向,而是在瀏覽器中的後退按鈕也起作用。
import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter, Switch, Route, Redirect} from 'react-router-dom';
import Signup from './components/Signup';
import Welcome from './components/Welcome';
class App extends React.Component {
constructor() {
super();
this.state = {
isLoggedIn: false
};
}
render() {
return (
<BrowserRouter>
<div>
<Route exact path="/signup" component={Signup}/>
<Route exact path="/welcome" component={Welcome}/>
<Route exact path="/" render={() => (
this.state.isLoggedIn ?
history.push('/welcome') : history.push('/signup')
)}/>
<Redirect to="/"/>
</div>
</BrowserRouter>
);
}
}
ReactDOM.render(<App/>, document.getElementById('root'));
這可能幫助:https://stackoverflow.com/questions/42474176/ using-conditional-component-with-the-same-route-path-in-reactjs – Dev
我已經看到了。它不影響這個歷史,它只是有條件地顯示組件。 –