Reactjs 从react路由器导入浏览器历史记录
我正在尝试导入browserHistory,但我遇到类似这样的错误Reactjs 从react路由器导入浏览器历史记录,reactjs,react-native,frontend,Reactjs,React Native,Frontend,我正在尝试导入browserHistory,但我遇到类似这样的错误 ./src/App.js Attempted import error: 'browserHistory' is not exported from 'react- router'. 我的代码是: import React, { Component } from "react"; import { browserHistory } from "react-router"; import App from "./A
./src/App.js
Attempted import error: 'browserHistory' is not exported from 'react-
router'.
我的代码是:
import React, { Component } from "react";
import { browserHistory } from "react-router";
import App from "./App";
class Home extends Component {
home = () => {
browserHistory.push("/home");
};
state = {};
render() {
return <button onClick={this.home} className="btn btn-outline-light">
Logout
</button>;
}
import React,{Component}来自“React”;
从“react router”导入{browserHistory};
从“/App”导入应用程序;
类Home扩展组件{
首页=()=>{
browserHistory.push(“/home”);
};
状态={};
render(){
返回
注销
;
}
}据我所知,您的主要目的是以正确的方式更改浏览器URL,而不是实际导入
浏览器历史记录
对于react路由器
v4:
您应该从HOCwith Router
中获益。以下一种方式应用它,这将传递到您的组件中一些额外的道具,这些道具将允许浏览浏览器历史记录
import { withRouter } from "react-router-dom";
class Home extends Component {
home = () => {
// - you can now access this.props.history for navigation
this.props.history.push("/home");
};
render() {
return (
<button onClick={this.home} className="btn btn-outline-light">
Logout
</button>;
)
}
const HomeWitRouter = withRouter(Home);
从“react router dom”导入{withRouter};
类Home扩展组件{
主页=()=>{
//-您现在可以访问this.props.history进行导航
this.props.history.push(“/home”);
};
render(){
报税表(
注销
;
)
}
const HomeWitRouter=withRouter(Home);
对于react路由器v5和react挂钩,这可能看起来像:
import { useHistory } from "react-router-dom";
export const Home = () => {
const history = useHistory();
const goHome = () => {
history.push("/home");
};
return (
<button onClick={goHome} className="btn btn-outline-light">
Logout
</button>;
)
}
从“react router dom”导入{useHistory};
export const Home=()=>{
const history=useHistory();
const goHome=()=>{
历史推送(“/主页”);
};
报税表(
注销
;
)
}
React-Router的哪个版本?在v4中,单个历史记录被删除并替换为路由器,即BrowserRouter
来自“React-Router-dom”
这可能解释了所有可能的重复,可能的重复,我正试图从一个页面重定向到另一个页面,当我搜索它时,我发现了这一个浏览器历史记录为什么您使用此HomWitOuter,它会给我警告,如此已定义,但其值从未使用过。您需要导出HomeWithRouter组件并使用它。或者使用它在这个文件中。