class component and functional component in react code example
Example 1: 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>);
}
Example 2: react functional component
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}