Build a React Counter Component
Filed under Cognizant
Free read 2 of 3The question
Implement a React component that displays a counter with buttons to increment, decrement, and reset the value.
Constraints
- Use React hooks (useState)
- State must update the UI correctly
- Clear button should reset count to 0
Examples
- Input
- Click Increment twice, then Clear
- Output
- Count: 0
- Why
- Incrementing twice makes count 2, then Clear resets it to 0.
Approach
Initialize a count variable using useState(0).
Define functions to update state: setCount(c => c + 1), setCount(c => c - 1), and setCount(0).
Return a JSX structure with the current count and three buttons attached to the handlers.
Tips
- What they are testing: Tests understanding of component state and event handling.
- Common mistake: Mutating state directly instead of using the setter function provided by useState.
Related Cognizant questions
Collected from public candidate posts and coding forums. Updated Nov 2026.