React Router V4로 업그레이드했지만 현재 history.push
방법으로 고생하고 있습니다. ReactJS 라우터 V4 history.push가 작동하지 않습니다.
import React from "react";
import { render } from "react-dom";
import { BrowserRouter, Route } from 'react-router-dom';
import createBrowserHistory from 'history/createBrowserHistory';
const history = createBrowserHistory();
import { Main} from "./components/Main";
import { About } from "./components/About";
import { Cars} from "./components/Cars";
class App extends React.Component {
render() {
return (
<BrowserRouter history={history}>
<div>
<Route path={"/"} component={Main} />
<Route path={"/cars"} component={Cars}/>
<Route path={"/about"} component={About}/>
</div>
</BrowserRouter>
)
}
}
render(<App/>, window.document.getElementById("app"));
과 그때는 다음과 같습니다 특정 페이지로 돌아갑니다 나는 간단한 추가 다른 파일이 있습니다
import React from "react";
import createBrowserHistory from 'history/createBrowserHistory';
const history = createBrowserHistory();
export class Cars extends React.Component {
navigateBack() {
history.push('/')
}
render() {
return(
<div>
<h3>Overview of Cars</h3>
<p>Model: </p>
<button onClick={this.navigateBack} className="btn btn-primary">Back</button>
</div>
)
}
}
그래서를 , 나는 여기서 무엇이 잘못되었는지 알 수 없다. 버튼을 클릭하면 URL이 /
로 변경되지만 모두 변경됩니다. 나를 도울 수있는 사람이 있습니까?
편집
나는이 작업을 수행 할 때 작동하는지, 발견 :
this.props.history.push('/home')
및
<button onClick={this.onNavigateHome.bind(this)}
하지만 어떻게 든 잘못된 것 같다 ??
내가 왜 Cannot read property 'context' of null
하지만 얻을
this.context.history.push('/home')
할 때? 내 <BrowserRouter>
설정이 잘못 되었습니까 ??
this.context.history.push('/cart');
이
체크 아웃이 게시물을 사용할 필요가 V4와
기본적으로 BrowserRouter에는 암시 적 기록이 있습니다. 대신 라우터를 사용해야합니다. 참조 : https://reacttraining.com/react-router/web/api/Router – Adriano
[React Router v4에서 기록으로 푸시하는 방법] (https://stackoverflow.com/questions/42701129/)과 중복되는 부분이있을 수 있습니다. 반응 - 라우터 - v4) – lux