This code, which tries to fetch data, will throw an error. What is the bug?

React JS interview question for Advanced practice.

Answer

The reducer function is marked async, which is not allowed.

Explanation

Reducer functions must be synchronous. They take the current state and an action and return the new state. You cannot perform asynchronous operations (like fetch) inside a reducer. This logic should be moved to a createAsyncThunk.

Related Questions