import PropTypes from 'prop-types';
class Greeting extends React.Component {
render() {
return (
<h1>Hello, {this.props.name}</h1>
);
}
}
Greeting.propTypes = {
name: PropTypes.string
};
import React from 'react';
import PropTypes from 'prop-types';
const Example = (props) => {
return (
<div>
<p>Some prop: {props.some}</p>
<p>Other prop: {props.other}</p>
</div>
);
};
Example.propTypes = {
some: PropTypes.string.isRequired,
other: PropTypes.number.isRequired
};
export default Example;
type Props = {
size: string;
}
const Component = ({ size = 'medium' }: Props) => (
<div className={cn('spinner', size)} />
);
// proptypes using class component
Detaljer.PropTypes = {
detaljer: PropTypes.string.isRequired,
feilkode: PropTypes.string,
removeEvent: PropTypes.string.isRequired
};
// proptypes using function component
Detaljer.propTypes = {
detaljer: PropTypes.string.isRequired,
feilkode: PropTypes.string,
removeEvent: PropTypes.string.isRequired
};
type OscarProp = {
children: React.ReactNode;
};
function App() {
return (<div>
<Message text="rando text" />
</div>
);
}
export default App;
type MessageProps ={
text: number
}
function Message({text}: MessageProps)
{
return(<div>
{text}
</div>)
}
/*pop quiz, will the example below work?
type MessageProps ={
text: number
}
Answer: no, will produce an error due to wrong type
*/
type OscarProp = {
children: React.ReactNode;
};
import PropTypes from 'prop-types'
function HelloWorldComponent({ name }) {
return (
<div>Hello, {name}</div>
)
}
HelloWorldComponent.propTypes = {
name: PropTypes.string
}
export default HelloWorldComponent
//You can now use `PropTypes.elementType`
// to validate for Component type props
type Person = {
name: {
first: string;
last: string;
};
};