Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,339 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);
}