1. Home
  2. /
  3. Next-js
  4. /
  5. Next.js Counter with useMemo

Next.js Counter with useMemo - Next-js Typing CST Test

Loading…

Next.js Counter with useMemo — Next-js Code

Uses useMemo to compute double of count for demonstration.

import { useState, useMemo } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  const [isDark, setIsDark] = useState(false);
  const double = useMemo(() => count * 2, [count]);

  return (
    <div className={isDark ? 'dark-theme' : 'light-theme'} style={{ padding: '2rem', textAlign: 'center' }}>
      <h2>Counter: {count} (Double: {double})</h2>
      <button onClick={() => setCount(count + 1)}>+</button>
      <button onClick={() => setCount(count - 1)}>-</button>
      <button onClick={() => setCount(0)}>Reset</button>
      <button onClick={() => setIsDark(!isDark)}>Switch Theme</button>
      <style jsx>{`
        .dark-theme { background: #222; color: #eee; min-height: 100vh; }
        .light-theme { background: #fff; color: #000; min-height: 100vh; }
        button { margin: 0 0.5rem; }
      `}</style>
    </div>
  );
}

Next-js Language Guide

Next.js is a powerful React framework for building modern full-stack web applications with features like server-side rendering, static generation, routing, API endpoints, and server components. It emphasizes performance, scalability, and developer experience.

Primary Use Cases

  • ▸SEO-optimized websites
  • ▸Full-stack React apps with server routes
  • ▸E-commerce storefronts
  • ▸Dashboards and SaaS apps
  • ▸Static or hybrid-rendered marketing sites

Notable Features

  • ▸App Router with React Server Components
  • ▸File-system routing
  • ▸API Routes + serverless functions
  • ▸SSR, SSG, ISR, CSR hybrid rendering
  • ▸Built-in image optimization

Origin & Creator

Created in 2016 by Guillermo Rauch and maintained by Vercel, designed to simplify server-rendered React applications.

Industrial Note

Next.js excels in enterprise-grade applications, JAMstack workflows, SEO-critical websites, dashboards, and scalable full-stack React platforms.

More Next-js Typing Exercises

Next.js Counter with useStateNext.js Counter with useEffect and localStorageNext.js Counter using useReducerNext.js Counter with ContextNext.js Counter with TypeScriptNext.js Counter with Inline StylesNext.js Counter with Buttons MapNext.js Counter with Custom Hook

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher