Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript React by Example Building the Application Creating the Counter

What does isRequired do exactly?

Guil decided in this lesson that the numberAttending, numberUnconfirmed and totalInvited would not have the isRequired tag

    Counter.propTypes = {
        numberAttending: PropTypes.number,
        numberUnconfirmed: PropTypes.number,
        totalInvited: PropTypes.number
    };

However, the counter is always present in the DOM. What does making these optional do exactly? Why aren't they required unlike almost all of the other propTypes in this course? Maybe I'm just not understanding what isRequired actually does?

1 Answer

Abraham Juliot
Abraham Juliot
47,353 Points

We may later decide to reuse and show the Counter component to not include props that compute values when the list is at an empty state.

isRequired will cause a warning to show if the prop is not provided. If you are interested, this is a snippet of isRequired in action from line 189 at https://github.com/facebook/prop-types/blob/master/factoryWithTypeCheckers.js

if (props[propName] == null) {
    if (isRequired) {
        if (props[propName] === null) {
            return new PropTypeError('The ' + location + ' `' + propFullName + '` is marked as required ' + ('in `' + componentName + '`, but its value is `null`.'));
        }
        return new PropTypeError('The ' + location + ' `' + propFullName + '` is marked as required in ' + ('`' + componentName + '`, but its value is `undefined`.'));
    }
    return null;
} else {
    return validate(props, propName, componentName, location, propFullName);
}