我想使用React路由器v4设置一个项目,我设法使它工作得很好。React路由器v4与路由器一起使用时抛出PropTypes错误
然而,当我尝试实施到重回巅峰,according to this docs滚动功能,有此错误:
Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.
我已经下载了阵营prop-types
包。只有在组件内部设置了Router()时才会出现此错误,例如:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router';
class ScrollTop extends Component {
componentDidUpdate(prevProps) {
if (this.props.location !== prevProps.location) {
window.scrollTo(0, 0);
}
}
render() {
return (
<div>{ this.props.children }</div>
);
}
}
export default withRouter(ScrollTop);
上面的代码将生成错误。但是,如果使用下面的代码:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class ScrollTop extends Component {
// ..same as code above..
}
export default ScrollTop;
这不出现错误,但也改变路由时不回顶部滚动。
我该如何解决这个问题?或者我做错了什么?