reactjs - 使用 withStyles 时 React Router 无法按预期工作
问题描述
我进行了快速搜索,但在 stackoverflow 中找不到相关的解决方案,但如果我错过了,请指引我到那里,并对重复感到抱歉。
我的问题是使用 withStyles 时反应路由器无法按预期工作。当您单击“主页”链接两次时,它会第二次显示空白页。如果我不使用“withStyles”并正常导出它,它就会按预期工作。
非常感谢您提前回答。
请看下面的代码。基本上“联系人”和“关于”的其他 2 个文件与 Home 相同,只是名称不同。
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import './css/index.css';
import App from './components/App';
import * as serviceWorker from './serviceWorker';
import { history } from './store';
ReactDOM.render((
<Router history={history}>
<App/>
</Router>),
document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
serviceWorker.unregister();
应用程序.js
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';
import Contact from './Contact';
import About from './About';
import Home from './Home';
import Header from './Header';
import '../css/App.css';
class App extends Component {
render() {
return (
<div>
<Header/>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" exact component={Contact} />
<Route component={Error} />
</Switch>
</div>
);
}
}
export default App;
页眉.js
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { makeStyles, withStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
},
menuButton: {
marginRight: theme.spacing(2),
},
title: {
flexGrow: 1,
},
link: {
margin: theme.spacing(1),
}
}));
class Header extends Component {
render() {
const classes = this.props;
return (
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link exact to="/about">About</Link>
</li>
<li>
<Link exact to="/contact">Contact</Link>
</li>
</ul>
</div>
);
}
}
export default withStyles(useStyles)(Header);
主页.js
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { makeStyles, withStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import Typography from '@material-ui/core/Typography';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router-dom';
const useStyles = makeStyles(theme => ({
root: {
padding: theme.spacing(3, 2),
},
}));
class Home extends Component {
constructor(props) {
super(props);
this.state = {
};
}
render() {
const { classes } = this.props
return (
<div>
<Paper className={classes.root}>
<Typography variant="h5" component="h3">
This is Home page
</Typography>
</Paper>
</div>
);
}
}
Home.propTypes = {
classes: PropTypes.object.isRequired
};
//just so you know I have used with both withRouter and without and its the same with
//both
//const HomeWithRouter = withRouter(Home);
//export default withStyles(useStyles)(HomeWithRouter);
export default withStyles(useStyles)(Home);
解决方案
makeStyles 是功能组件的钩子,您正在使用带有 HOC 的类组件。
改变
const useStyles = makeStyles(theme => ({
root: {
padding: theme.spacing(3, 2),
},
}));
到
const styles = theme => ({
root: {
padding: theme.spacing(3, 2),
},
});
然后改变
export default withStyles(useStyles)(Home);
到
export default withStyles(styles)(Home);
为您的 Home 和 Header 组件执行此操作。老实说,我可能只是将类转换为带有钩子的功能组件,但无论哪种方式,您都必须使用钩子或类,但不能将两者混合使用。
推荐阅读
- python - RuntimeError: 3 维权重 [128, 1024, 1] 的预期 3 维输入,但得到了大小为 [32, 1024] 的 2 维输入
- azure-devops - Azure DevOps 服务器 - 多个应用程序层
- plugins - 我的 QuickBooks Online 应用程序是否需要在单独的窗口或选项卡中运行?是否有像插件一样运行的应用程序,在与 QB Online 相同的窗口中?
- html - 为什么会出现水平滚动条?
- r - 将多列合并为一个列表列
- python - 如何使用 pandas 从 CSV 打印出特定列
- spring-boot - 使用多个数据源的 LazyInitializationException
- java - ref 的字符串赋值。将对象字符串到内存中会发生在此语句之后吗?
- php - 无法在某些子进程成为僵尸之前终止它们
- android - 如何为 Jenkins 配置 docker 镜像以创建 Android 构建?