Hone logo
Hone
Problems

Building Action Creators for a Simple Counter App

Action creators are a fundamental pattern in Redux (and similar state management libraries) that simplify the process of dispatching actions. This challenge asks you to implement action creators for a basic counter application, demonstrating your understanding of how to encapsulate action logic and return action objects. This is a crucial skill for managing state effectively in React applications.

Problem Description

You are tasked with creating action creators for a simple counter application. The application has three possible actions: increment, decrement, and reset. Each action creator should return a properly formatted action object with a type property and, for increment and decrement, a payload property representing the amount to increment or decrement.

What needs to be achieved:

  • Implement three action creators: incrementActionCreator, decrementActionCreator, and resetActionCreator.
  • Each action creator should return a JavaScript object representing an action.
  • The action objects must adhere to a specific structure (see below).

Key Requirements:

  • incrementActionCreator should return an action object with type: 'INCREMENT' and payload: number. The payload should default to 1 if no argument is provided.
  • decrementActionCreator should return an action object with type: 'DECREMENT' and payload: number. The payload should default to 1 if no argument is provided.
  • resetActionCreator should return an action object with type: 'RESET'. It should not accept any arguments.

Expected Behavior:

  • Calling incrementActionCreator() should return { type: 'INCREMENT', payload: 1 }.
  • Calling incrementActionCreator(5) should return { type: 'INCREMENT', payload: 5 }.
  • Calling decrementActionCreator() should return { type: 'DECREMENT', payload: 1 }.
  • Calling decrementActionCreator(3) should return { type: 'DECREMENT', payload: 3 }.
  • Calling resetActionCreator() should return { type: 'RESET' }.

Edge Cases to Consider:

  • The payload for incrementActionCreator and decrementActionCreator should be a number. While not strictly enforced in this challenge, consider how you might handle non-numeric inputs in a real-world scenario (e.g., throwing an error or defaulting to 1).
  • Ensure the action types are strings.

Examples

Example 1:

Input: incrementActionCreator()
Output: { type: 'INCREMENT', payload: 1 }
Explanation: Called incrementActionCreator with no arguments, so the default payload of 1 is used.

Example 2:

Input: decrementActionCreator(2)
Output: { type: 'DECREMENT', payload: 2 }
Explanation: Called decrementActionCreator with a payload of 2.

Example 3:

Input: resetActionCreator()
Output: { type: 'RESET' }
Explanation: Called resetActionCreator with no arguments, returning the reset action.

Constraints

  • The action types must be strings: 'INCREMENT', 'DECREMENT', and 'RESET'.
  • The payload (when present) must be a number.
  • The code must be written in TypeScript.
  • The solution should be concise and readable.

Notes

  • Think about how to use default parameters in your functions to handle cases where no arguments are provided.
  • This challenge focuses on the creation of action objects, not on connecting them to a Redux store or handling state updates. You are only responsible for the action creator functions themselves.
  • Consider using type annotations to improve code clarity and maintainability.
Loading editor...
typescript