对于一个项目,我们希望通知在应用程序的所有 View 中可见。
侧边栏菜单允许用户在 View 之间移动,与通知组件和websocket组件处于同一级别react-stomp 。路由器用于根据所选的菜单项更改下一级的 View 。
我们的问题是,如果有新消息到达,我们需要将其作为 props 传递给 View ,当然这会触发整个组件的重新渲染。
有办法避免这种重新渲染吗?
我们希望避免 redux,尽管我们知道这是一个可能的解决方案。
最佳答案
默认情况下,如果 props 发生变化,react 会触发重新渲染。如果您想避免渲染组件,可以使用钩子(Hook) shouldComponentUpdate(nextProps, nextState)
引用React的文档:
Use shouldComponentUpdate() to let React know if a component’s output is not affected by the current change in state or props. The default behavior is to re-render on every state change, and in the vast majority of cases you should rely on the default behavior.
欲了解更多信息,请访问their docs
关于javascript - React 避免重新渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51234142/