🌱 Digital garden
  • Welcome
  • Courses
    • Fundamentals of 2D Game Engines with C++ SDL and Lua
      • Dependencies and project structure
  • Computer science
    • Basic
  • Sound
    • FM synthesis
  • Development
    • Design system
    • Text editors
      • Vim
      • Emacs
      • VS Code
    • Databases
      • MongoDB
    • NES Development
    • Programming languages
      • Shell
      • Javascript
        • Arrays
        • JSDoc
    • UML
    • Web development
      • HTTP
      • GraphQL
      • PWA
      • CSS
      • REST
      • Web scrapping
      • React
        • React hooks
      • HTML5
        • Canvas
          • WebGL
          • Context2D
    • Game development
      • Pixel art
      • AABB collision
      • Sprite
      • Game loop
  • Education
    • Tools
      • Anki
  • Electronic
    • Components
      • Opto-isolator
  • Image quality
  • Languages
    • English
  • Utilities
    • Docker
    • SSH
  • Lifestyle
    • Minimalism
    • Reading
  • Science
    • Math
      • Fractions
  • Productivity
    • Bullet journaling
    • Typing
  • Operating system
    • Unix
    • Linux
    • MacOS
  • Learning resources
  • Research
Powered by GitBook
On this page
  • Notes
  • useState
  • useReducer
  • Links

Was this helpful?

  1. Development
  2. Web development
  3. React

React hooks

Notes

useState

import React, { useState } from 'react'

export const App = () => {
  const [state, setState] = useState({
    count: 0
  })

  return (
    <button
      onClick={() => setState((prev) => ({ ...prev, count: state.count + 1 }))}
    >
      Increment
    </button>
  )
}

useReducer

import React, { useReducer } from 'react';

const reducer = (state, action) => {
    switch(action.type) {
        case 'CHANGE_NAME':
            return {...state, name: action.payload.name}
        default:
            return {...state}

export const App = () => {
    const [state, dispatch] = useReducer(reducer, { name: '' })

    return (
        <button
            onClick={() => {
                dispatch({
                    action: 'CHANGE_NAME',
                    payload: 'John'
                })
            }}
        >
            Change name
        </button>
    )

Links

PreviousReactNextHTML5

Last updated 4 years ago

Was this helpful?

https://www.youtube.com/playlist?list=PLZlA0Gpn_vH8EtggFGERCwMY5u5hOjf-h