Last active
March 27, 2020 19:49
-
-
Save tudorilisoi/c2e361493f4db82354de85001d508cb1 to your computer and use it in GitHub Desktop.
a React propTypes example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import slugify from 'slugify'; | |
import pTypes from 'prop-types'; | |
// This object will allow us to | |
// easily convert numbers into US dollar values | |
// React propTypes | |
const USCurrencyFormat = new Intl.NumberFormat('en-US', { | |
style: 'currency', | |
currency: 'USD' | |
}); | |
export default class FeatureItem extends Component { | |
static propTypes = { | |
//unique id | |
id: pTypes.string.isRequired, | |
onChange: pTypes.func.isRequired, | |
//displayed name | |
name: pTypes.string, | |
} | |
render() { | |
const { id, name, checked, onChange } = this.props | |
return ( | |
<div key={id} className="feature__item"> | |
<input | |
type="radio" | |
id={id} | |
className="feature__option" | |
name={name} | |
checked={checked} | |
onChange={onChange} | |
/> | |
<label htmlFor={id} | |
className="feature__label"> | |
{this.props.item.name} ({USCurrencyFormat.format(this.props.item.cost)}) | |
</label> | |
<Greeting name={'Seth'} age={32} /> | |
</div> | |
); | |
} | |
} | |
function Greeting({name, age}) { | |
return <p>{`Hello, ${name} , ${age}`}</p> | |
} | |
Greeting.propTypes = { | |
name: pTypes.string.isRequired, | |
age: pTypes.number.isRequired, | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment