jeudi 12 décembre 2019

Why does mutation make a react component non-reusable on it's own?

So I'm reading the react doc on HOC's (found here: https://reactjs.org/docs/higher-order-components.html) and there's this section:

"Resist the temptation to modify a component’s prototype (or otherwise mutate it) inside a HOC."

function logProps(InputComponent) {
  InputComponent.prototype.componentWillReceiveProps = function(nextProps) {
    console.log('Current props: ', this.props);
    console.log('Next props: ', nextProps);
  };
  // The fact that we're returning the original input is a hint that it has
  // been mutated.
  return InputComponent;
}

// EnhancedComponent will log whenever props are received
const EnhancedComponent = logProps(InputComponent);

"There are a few problems with this. One is that the input component cannot be reused separately from the enhanced component."

Why is the mutated input component not considered reusable while the wrapped component is?

Aucun commentaire:

Enregistrer un commentaire