【问题标题】:Pure Component renders when no change is there?没有变化时呈现纯组件?
【发布时间】:2020-09-24 16:16:37
【问题描述】:

我有一个像这样的纯组件?

interface Props {
 checkBoxTitleStyle?: any
  checkBoxBackgroundColor?: any
  onPressCheckBox?: (id, isChecked, selectedArray , options?: CheckBoxOptions) => void
  itemKey?: any
  mainContainerStyle?: any
}

class CheckBoxComponent extends PureComponent<Props> {
constructor()
render()
}

现在当我在我的 otherComponent 中使用这个 pureComponents

   <CheckBoxComponent
          checkBoxKey={checkBoxKey}
          itemKey={get(item , 'id')}
          itemTitle={get(item , 'label', '')}
          isCheckBoxSelected={get(item , 'isChecked' , '')}
          checkBoxBackgroundColor={colors.DuckBlue}
        />

如果我不通过道具 mainContainerStyle 那么它工作正常,它只在有一些变化时呈现。

但是如果我在 props 中传递 mainContainerStyle,那么即使没有变化,它也会每次都渲染。每次渲染都会使性能变慢。有什么方法可以解决它或为什么会这样。

【问题讨论】:

标签: reactjs react-native react-redux react-router


【解决方案1】:

确保不要传递给像 mainContainerStyle={{...other stuff...}} 这样的 prop 字面量对象,而是保存它的状态。

示例

const { PureComponent, useState, useEffect } = React;

class CheckBoxComponent extends PureComponent {
  constructor(props) {
    super(props);
  }
  
  render() {
    console.log('render');
    return <div>{JSON.stringify(this.props)}</div>
  }
}

const App = () => {
  const [mainContainerStyle, setStyle] = useState({position: 'absolute'});
  const [count, setCount] = useState(0);
  const checkBoxKey = 1;
  const item = {
    id: 1,
    label: 'label',
    isChecked: true,
    options: {}
  }
  const [options, setOptions] = useState(options);
  const [itemState, setItemState] = useState(item);
  const colors = {
    DuckBlue: ''
  }
  const get = (item, prop) => {
    return item[prop]
  }
  

return <div>
    <CheckBoxComponent
       checkBoxKey={checkBoxKey}
        itemKey={get(item , 'id')}
        itemTitle={get(item , 'label', '')}
        isCheckBoxSelected={get(item , 'isChecked' , '')}
        checkBoxBackgroundColor={colors.DuckBlue}
        //mainContainerStyle={{}} // causes re-render
        mainContainerStyle={mainContainerStyle} // state
        //options={get(item , 'options')} // causes re-render
        //options={itemState.options} // or
        options={options}
    />
    <button onClick={() => setCount(count => count + 1)}>Change Style</button>
  </div>
}

ReactDOM.render(
    <App />,
    document.getElementById('root')
  );
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<div id="root"></div>

【讨论】:

  • 我可以为这样的主容器样式道具做。但是我的 purecomponent 中也有一个 prop 选项。 stackoverflow.com/questions/62218097/…
  • 更新了答案。同样的故事。 options 属性必须使用 useState
  • 绝对没有理由为此使用状态。导致重新渲染问题的原因是您已正确识别的对象的内联创建。但解决方案不是将其添加到状态中。状态是随时间变化的值。这个值没有。相反,为App 函数的outside 对象创建一个const。这不会在每次渲染时创建一个新对象,因此不会触发重新渲染,但也不会创建不必要的状态值。
猜你喜欢
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 2015-05-26
相关资源
最近更新 更多