Skip to content

Instantly share code, notes, and snippets.

@debonx
Last active January 2, 2020 17:21
Show Gist options
  • Save debonx/da429d37bf875cb6ab8b902b44c9d571 to your computer and use it in GitHub Desktop.
Save debonx/da429d37bf875cb6ab8b902b44c9d571 to your computer and use it in GitHub Desktop.
React: How to set propTypes stateless component.
import React from 'react';
export class BestSeller extends React.Component {
render() {
return (
<li>
Title: <span>
{this.props.title}
</span><br />
Author: <span>
{this.props.author}
</span><br />
Weeks: <span>
{this.props.weeksOnList}
</span>
</li>
);
}
}
// 1. Define propTypes as static object literal
// 2. The name of each property in propTypes should be the name of an expected prop
BestSeller.propTypes = {
title: React.PropTypes.string.isRequired,
author: React.PropTypes.string.isRequired,
weeksOnList: React.PropTypes.number.isRequired
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment