DEV Community

Cristian Sifuentes
Cristian Sifuentes

Posted on

Building a React Counter with TypeScript: From useState to Mathematical Insight

Building a React Counter with TypeScript

Building a React Counter with TypeScript: From useState to Mathematical Insight

In modern React development, managing state efficiently with clarity and type safety is essential. One of the simplest yet powerful tools in our arsenal is the useState hook.

In this article, we’ll walk through a real-world React + TypeScript example using useState, explore how to handle TypeScript configurations, and use this to illustrate a mathematical conceptlinear growth.


What is useState?

useState is a hook that lets you store state within a functional component. Each time the state changes, the component re-renders.

import { useState } from 'react';

export const Counter = () => {
  const [counter, setCounter] = useState(0);

  const increment = (num: number): void => {
    setCounter(counter + num);
  }

  return (
    <div className='mt-5'>
      <h3>Counter: useState</h3>
      <span>Value: { counter }</span>
      <br/>
      <button onClick={() => increment(1)} type="button" className="btn btn-primary">+1</button>
      <button onClick={() => increment(2)} type="button" className="btn btn-primary">+2</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Fixing TypeScript Error: 'Parameter 'num' implicitly has an 'any' type'

This error appears when TypeScript's strict mode is enabled and a parameter type is not explicitly defined.

Fix:

const increment = (num: number): void => { ... }
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can disable strict mode (not recommended for large-scale projects):

// tsconfig.json
{
  "compilerOptions": {
    ...
    "strict": false
  }
}
Enter fullscreen mode Exit fullscreen mode

Connecting it to a Mathematical Concept: Linear Growth

In this counter example, each click increases the value by a constant number. This is an example of a linear function in mathematics:

f(x) = x + a

Where:

  • x is the current counter value
  • a is the increment step (1 or 2 in our case)

Each press applies:

newCounter = currentCounter + step;
Enter fullscreen mode Exit fullscreen mode

This mirrors the equation:

f(x) = x + k
Enter fullscreen mode Exit fullscreen mode

Why This Matters

Understanding this helps developers:

  • Visualize application state as mathematical models.
  • Think predictably about growth and state transitions.
  • Lay the groundwork for more advanced logic like useReducer or animations based on physics or algebra.

Pro Tips for React Developers

  • Always type your function arguments: num: number
  • Separate UI logic from state logic for clean, testable code.
  • Integrate real-world concepts (like math) to improve clarity in your state transitions.

Conclusion

React's useState is more than a counter—it's a window into functional programming and mathematical thinking.

By understanding linear growth through simple counters, you strengthen your mental model for more complex state updates, animations, or physics-based UIs.

React is declarative, but your thinking should be too—clear, incremental, and grounded in logic.


Tags: react typescript useState mathematics frontend

Top comments (1)