gpt4 book ai didi

javascript - 阻止的更新 : react router and redux

转载 作者:行者123 更新时间:2023-11-30 14:19:50 25 4
gpt4 key购买 nike

路由器 docs了解何时将 withRouter 与 redux 一起使用。但是,我发现它们并不完全清楚。

例如文档说在什么时候使用withRouter

  1. The component is connected to redux via connect()(Comp).
  2. The component is not a "route component", meaning it is not rendered like so:

一些我不清楚的事情是:

  • 首先,上面的第 1 点和第 2 点是否与 AND 相关?例如第一次和第二次发生时组件不会更新吗? (或者是 OR?)
  • 在第一种情况下,这是否意味着当我有 connect(mapStateToProps)(Comp) 时可以不使用 withRouter
  • 第二点我完全不清楚。这是否意味着我们应该将 withRouter 应用于任何不是“路由组件”的组件(是否连接 connect?)?

更新:经过一些exploration看来这在路由器 4.4 版本中应该不再是问题了。

最佳答案

In the first case does it mean when I have connect(mapStateToProps)(Comp) it is fine to not use withRouter?

connect()(Comp) 指定它是连接组件,这并不意味着 withRouter 可能不需要与其他 connectconnect(mapStateToProps)(Comp) 这样的参数。 The documentation也提到了这一点:

// before
export default connect(mapStateToProps)(Something)

// after
import { withRouter } from 'react-router-dom'
export default withRouter(connect(mapStateToProps)(Something))

Does it mean we should apply withRouter to ANY component (connect-ed or not?) which is not a "route component"?

如果组件不是路由组件,我们应该将 withRouter 应用于组件,我们希望它在位置更新时更新,即它使用 route props某种程度上来说。一个流行的例子是导航菜单,其中事件项目被突出显示,它不是路由组件但它需要知道当前路由。

Or why not attach withRouter to ANY connected component, will not it cover all cases?

这将为可能不需要路由事件的组件提供不必要的更新,因为它们不使用路由属性。

关于javascript - 阻止的更新 : react router and redux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52949653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com