1. Home
  2. /
  3. React
  4. /
  5. LocalStorage Counter

LocalStorage Counter - React Typing CST Test

Loading…

LocalStorage Counter — React Code

Persists counter value in localStorage using useEffect.

import React, { useState, useEffect } from 'react';

const LocalStorageCounter = () => {
	const [count, setCount] = useState(0);
	useEffect(() => {
		const saved = localStorage.getItem('count');
		if (saved) setCount(parseInt(saved, 10));
	}, []);
	useEffect(() => {
		localStorage.setItem('count', count);
	}, [count]);
	return (
		<div>
		<h2>Count: {count}</h2>
		<button onClick={() => setCount(c => c + 1)}>+</button>
		<button onClick={() => setCount(c => c - 1)}>-</button>
		</div>
	);
};

export default LocalStorageCounter;

React Language Guide

React is a declarative, component-based JavaScript library for building user interfaces, primarily for single-page applications. It allows developers to create reusable UI components and manage application state efficiently.

Primary Use Cases

  • ▸Single-page web applications (SPAs)
  • ▸Dynamic user interfaces for web apps
  • ▸Mobile apps via React Native
  • ▸Reusable component libraries
  • ▸Interactive dashboards and admin panels

Notable Features

  • ▸Component-based architecture
  • ▸Virtual DOM for optimized rendering
  • ▸JSX syntax for combining HTML and JS
  • ▸One-way data binding (props)
  • ▸Hooks for state and lifecycle management

Origin & Creator

Developed by Jordan Walke at Facebook in 2013.

Industrial Note

React is specialized for building dynamic, responsive web and mobile UIs, particularly in SPA and complex front-end applications.

More React Typing Exercises

React Counter with useStateReact Theme Toggle with ContextReact Todo List with useStateReact Fetch Data with useEffectReact Form HandlingReact Modal ComponentReact useReducer CounterReact Context Counter ExampleReact Tab Component

Practice Other Languages

CPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypherGremlin