react class and functional components code example
Example 1: create-react-app class component
npx create-react-app my-app --scripts-version react-scripts@2.1.7
Example 2: class to functional component react
// convert a class component to a functional component
class MyComponent extends React.Component {
state: {
name: 'Bob'
}
render() {
return (
<p>Hello, my name is {this.state.name}</p>
);
}
}
const MyComponent = () => {
const [name, setName] = React.useState('Bob');
return (<p>Hello, my name is {name}</p>);
}