【发布时间】:2021-11-20 14:12:53
【问题描述】:
我对@987654321@ 表示怀疑,它说“历史对象是可变的”,并给出了如下示例:
历史对象是可变的。因此建议访问
location来自<Route>的渲染道具,而不是来自history.location。这可以确保你对 React 的假设是 在生命周期钩子中正确。例如:class Comp extends React.Component { componentDidUpdate(prevProps) { // will be true const locationChanged = this.props.location !== prevProps.location; // INCORRECT, will *always* be false because history is mutable. const locationChanged = this.props.history.location !== prevProps.history.location; } } <Route component={Comp} />;
在上面的示例代码中,这两个地方不应该是===而不是!==吗?
【问题讨论】:
-
这正是它所说的:历史对象是可变的,即它的值可以在不改变其身份的情况下改变。不,它是
!==,因为正如变量名所暗示的那样,它正在检查位置中的更改。
标签: reactjs react-router react-router-dom