Wayne Bloss
Wayne Bloss

Reputation: 5550

Does it make a difference if you don't use a prop in your PureComponent render method?

Say I have a PureComponent with a bunch of props. If I don't use every single prop in my render method, will it affect anything at all?

For instance, if I depend on componentWillReceiveProps being called for every single prop, even the ones that are not rendered...it will still be called, correct?

I can't imagine the React authors doing anything else... I can probably hunt down the code and take a peek myself and I will if I can't get a definitive answer here. Thanks.

NOTE: The reason I'm asking is because I'm wondering I see some code that is pulling unused variables from props here.

Upvotes: 0

Views: 61

Answers (2)

Oro
Oro

Reputation: 1867

PureComponent is just like a regular component, only it implements the lifecycle method shouldComponentUpdate(). This function will ultimately determine if render() will run, so it cannot possibly rely on render() itself.

For instance, if I depend on componentWillReceiveProps being called for every single prop, even the ones that are not rendered...it will still be called, correct?

Yes, that is correct.


The code you referenced contains a common pattern, where props should be passed down, but not as a whole. The omitted props are commonly known as ownProps, which indicates that they belong solely to the component itself.

If we look at the code:

const {
    element,
    onChange: _onChange,
    value: _value,
    minLength: _minLength,
    //...
    ...props
 } = this.props;

In this case, value, minLength and debounceTimeout are only assigned to variables so they will be excluded from the props variable, which is used afterwards.

return React.createElement(element, {
    ...props,
    //...
}

Upvotes: 1

Dave L.
Dave L.

Reputation: 9781

For instance, if I depend on componentWillReceiveProps being called for every single prop, even the ones that are not rendered...it will still be called, correct?

Yes componentWillReceiveProps will still be called regardless of what effect a particular prop may or may not have on rendering.

even the ones that are not rendered

Note many props are not rendered themselves, but they may have some effect on rendering (or they may not). React would not know if a particular prop affected rendering at least until the new virtual DOM was constructed.

Upvotes: 1

Related Questions