mediumFrontend EngineerHealthtech
What is the difference between useState and useReducer in React? When would you choose one over the other?
Posted 18/04/2026
by Mehedy Hasan Ador
Question Details
At a health-tech startup, the interviewer asked:
> "We have a multi-step form with 5 steps. Each step has multiple fields and validation. Some fields affect other steps (e.g., selecting 'International' in step 1 shows different fields in step 3). The state management is becoming messy with 15+ useState calls. How would you refactor this?"
> "We have a multi-step form with 5 steps. Each step has multiple fields and validation. Some fields affect other steps (e.g., selecting 'International' in step 1 shows different fields in step 3). The state management is becoming messy with 15+ useState calls. How would you refactor this?"
Suggested Solution
useState vs useReducer
useState — Simple Independent State
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [step, setStep] = useState(1);
// Great for independent, simple values
useReducer — Complex Interdependent State
const initialState = {
step: 1,
personalInfo: { name: '', email: '' },
address: { country: 'BD', international: false },
preferences: {},
errors: {},
};
function formReducer(state, action) {
switch (action.type) {
case 'SETFIELD':
return {
...state,
[action.section]: {
...state[action.section],
[action.field]: action.value,
},
};
case 'SETCOUNTRY':
return {
...state,
address: { ...state.address, country: action.value },
// Derived state logic centralized here
preferences: action.value !== 'BD'
? { ...state.preferences, needsVisa: true }
: state.preferences,
};
case 'NEXTSTEP':
return { ...state, step: state.step + 1, errors: {} };
case 'SETERRORS':
return { ...state, errors: action.errors };
default:
return state;
}
}
function MultiStepForm() {
const [state, dispatch] = useReducer(formReducer, initialState);
return (
<div>
<StepIndicator step={state.step} />
{state.step === 1 && <PersonalStep data={state.personalInfo} dispatch={dispatch} />}
{state.step === 2 && <AddressStep data={state.address} dispatch={dispatch} />}
{/* ... */}
</div>
);
}
When to Use Which
Testing the Reducer
import { formReducer } from './formReducer';
test('setting international country enables visa', () => {
const result = formReducer(
{ address: { country: 'BD' }, preferences: {} },
{ type: 'SET_COUNTRY', value: 'US' }
);
expect(result.preferences.needsVisa).toBe(true);
});
Pure functions = no React rendering needed for tests.