I started React by learning about the useState
hook to manage component states. However, I have recently discovered that it is possible to use a state object instead. I'm confused, and can't seem to find many references on the internet to help me understand the difference.
What are the differences, if any between the following... And which one is preferred, if at all?
Option 1: State
object:
class User extends React.component {
state = {
username: 'Tyler'
}
Option 2: useState
hook:
class User extends React.component {
const [state, setState] = useState({
username: 'Tyler'
})