react class component vs functional components code example
Example 1: functional component react with state
import React, { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count" const [count, setCount] = useState(0);
return (
You clicked {count} times
);
}
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 (
Hello, my name is {this.state.name}
);
}
}
const MyComponent = () => {
const [name, setName] = React.useState('Bob');
return (Hello, my name is {name}
);
}