Open in app
remarkablemark
148 Followers
About

Sign in

148 Followers
About
Open in app

React state comparison: class vs function

remarkablemark

remarkablemark

·Mar 20

YouTube:

CodeSandbox:

Class

Rendering initial state:

Updating state:

Class state property must be an object since setState takes an object of state variables to update.

Function

Rendering initial state:

Updating state:

There can be multiple useState hooks in a function.

The initial state passed to useState doesn’t have to be an object:

Resources

  • YouTube video
  • CodeSandbox demo
  • State and Lifecycle
  • Using the State Hook

This article was originally published on remarkablemark.org on March 20, 2021.

  • React
  • React Hook
  • Reactjs
  • JavaScript
  • Coding

More from remarkablemark

remarkablemark.org

More From Medium

Cursor Custom Hook: Spice up That Mouse Cursor

Seun Adesina in The Startup

Building CI/CD for React Static Web Apps Service Using Azure DevOps

Bhargav Bachina in Bachina Labs

In-depth React

Walter Code

JSON Object v. JavaScript Object

Alyssa E Easterly in Geek Culture

JavaScript Memoization Patterns

Taha El Bouffi in Nerd For Tech

Tutorial: Full Stack App

Feather in The Startup

Is JavaScript the Best Language for New Coders?

Matthew MacDonald in Young Coder

Use a button to upload files on your React App (with bootstrap)

Prince Shrestha in CodeX

About

Help

Legal

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store