Track a Request with a Reducer
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Model a data request as a small state machine. Export initialRequest, the object { status: "idle", data: null, error: null }, and a reducer requestReducer(state, action) that returns the next state; status is "idle", "loading", "success" or "error". { type: "start" } begins a load from idle, success or error, giving { status: "loading", data: null, error: null }. { type: "succeed", data } finishes a load with { status: "success", data, error: null }, and { type: "fail", error } with { status: "error", data: null, error }. { type: "reset" } goes back to idle from any status. An action that doesn't fit the current status (start while loading, succeed or fail when nothing is loading) or has an unknown type is ignored: return the same state object. Never modify the state you're given. The default export App loads a quote with it in the preview; the checks test requestReducer itself.
Your code must pass
- start begins a load from idle, success or error
- succeed and fail finish a load
- Ignores actions that don't fit the status
- reset goes back to idle from any status
- Never changes the state it's given