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

ian izaguirre
ian izaguirre
3,220 Points

Question on how to write React.PropTypes syntax correctly?

On TreeHouse and on a lot of examples online, I have seen React.PropTypes written like this:

Application.propTypes = {
 title: React.PropTypes.string.isRequired
};

BUT I have seen one example somewhere else, write it this way:

import React from 'react';
import { string } from 'prop-types';

const ShowCard = props => (
    <div className="show-card">
        <img alt={`${props.title} Show Poster `} src={`/public/img/posters/${props.poster}`} />
        <div>
            <h3>{props.title}</h3>
            <h4>({props.year})</h4>
            <p>{props.description}</p>
        </div>
    </div>
);

ShowCard.propTypes = {
    poster: string.isRequired,
    title: string.isRequired,
    year: string.isRequired,
    description: string.isRequired
};

export default ShowCard;

I can see in the last example that proptypes was written as:

ShowCard.propTypes = {
    title: string.isRequired
};

which Does NOT have React.PropTypes.XXX written in front. So I was wondering why the last example can exclude writing title: React.PropTypes.string.isRequired and just write it as title: string.isRequired?

  • Are both way correct? Is one way better? How come the last way does not give an error?

1 Answer

React.PropTypes has been deprecated as of React 15.5. Using PropTypes is the correct way now.