我想使用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;
這不出現錯誤,但也改變路由時不回頂部滾動。
我該如何解決這個問題?或者我做錯了什麼?