The reason why your Header
component is being rendered again after running the changeTab
function is because you're calling the setState
function of a React.Component
.(运行changeTab
函数后再次渲染Header
组件的原因是因为您正在调用React.Component
的setState
函数。)
This function runs a re-render of the component it's calling on and all its children.(此函数对正在调用的组件及其所有子组件进行重新渲染。)
A quote from the documentation :(来自文档的报价:)
setState() enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the updated state.(setState()使更改进入组件状态并告诉React该组件及其子级需要使用更新后的状态重新呈现。) This is the primary method you use to update the user interface in response to event handlers and server responses.(这是用于响应事件处理程序和服务器响应而更新用户界面的主要方法。)
Your Header
component has nothing to do with the state of the parent component.(您的Header
组件与父组件的状态无关。) But it's re-rendered because it's the child of the parent component.(但这是因为它是父组件的子组件而被重新渲染。)
If you don't want your Header component to be re-rendered upon clicking the changeTab function, you should consider to move the logic of your tabs to a different component, or moving the Header component up the dom-tree (if possible).(如果您不希望在单击changeTab函数时重新呈现Header组件,则应考虑将选项卡的逻辑移动到其他组件,或者将Header组件移到dom-tree上(如果可能)。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…