Top 10+ Redux Interview Questions

Congratulations on being shortlisted for the interview! Now your focus should be on cracking your interview, we want to tell a bit about Redux and its scope. Redux works as an open-source JavaScript library that is used for managing the application state. It is written in JavaScript and it was created by Dan Abramov and Andrew Clark. Also, the scope of redux is increasing day by day as it makes the state changes in apps more predictable and hence results in the easy testing of the app. If you are looking for React Interview Questions then you can visit here.

Most Frequently Asked Redux Interview Questions

There are three core principles that Redux follows:

  • Single source of truth: The global state of your app is put away in an object tree inside a single store.
  • The state is read-only: State can only be changed by emitting an action, an object that explains what has happened.
  • Changes are made with pure functions: This is to define how the state tree is being transformed by the actions, you have to write pure reducers.

2. Do you need to keep all component states in the Redux store?

You do not need to push everything in the redux store as you have to keep your application state as small as possible. You should only do it if it makes a difference to you to keep something there or maybe helping you in making your life easier while using Dev Tools.

3. What is Redux DevTools? Also, explain the features of Redux DevTools?

It is a time travel environment that allows live editing for Redux with action replay, hot reloading, and customizable UI. For your ease, you can also use the extension of Redux DevTools in any of your browsers, Chrome, or firefox.

4. What is an action in Redux?

Actions are the plain JavaScript objects which contain a type field. Action can be considered as an event that can describe something that has happened in the application.

Always remember actions should contain a small amount of information that is needed to mention what has happened.

If you want to read more Interview questions related to Redux then you can visit here.

5. What is “store” in redux?

The Redux “store” carries together all the states, reducers, and actions that create the app. The store has multiple responsibilities:

  • It holds the state of the current application from inside
  • With the help of store.getState(); it allows access to the current state.
  • With the help of the store.dispatch(action); it allows the state to be updated.
  • With the help of the store.subscriber(listener); it allows to register listener callbacks.

Store Methods

  • getState()
  • dispatch(action)
  • subscribe(listener)
  • replaceReducer(nextReducer)

Example

import { createStore } from 'redux'
const store = createStore(todos, ['Use Redux'])
store.dispatch(addTodo('Read the docs'))
store.dispatch(addTodo('Read about the middleware'))

6. How to add multiple middlewares to Redux?

For adding multiple middlewares to Redux, you can use applyMiddleware by which the developer can pass each piece of middleware as the new or another argument. As per your preferences, you just need to pass every single piece of middleware.

For instance, one can add the Redux Thunk and the logger middleware as the argument just as below:

Example

import { createStore, applyMiddleware } from 'redux'
const createStoreWithMiddleware = applyMiddleware(ReduxThunk, logger)(createStore);

7. How to structure Redux top-level directories?

All the applications have multiple top-level directories as mentioned below:

  • Components: it is used for “dumb” React components that are unfamiliar with Redux.
  • Containers: It is used for “smart” React components which are connected to the Redux.
  • Actions: It is used for all the action creators, where the file name should be corresponding to the part of the app.
  • Reducers: It is used for all the reducers where the file name is corresponding to the state key.
  • Store: it is used for store initialization. This directory works best in small and mid-level size apps.

8. What are the downsides of Redux compared to Flux?

Instead of downsides, there are few compromises of using Redux over Flux that is listed below:

  • You need to learn the avoiding of mutations: Flux is un-opinionated about mutating the data, however, Redux does not like mutations, and most of the packages which are complementary to Redux should never alter the state.
  • You have to carefully pick your packages: While Flux principle does not try to solve the problems such as undo or redo, persistence, or the forms where Redux has extension points like store enhancers and middleware.
  • No nice Flow integration yet: Flux allows you to do impressive static type checks that Redux does not support yet.

9. What are reducers in redux?

The reducers in redux are the pure functions that take the previous state and an action, and then it returns to the next state.
(previousState, action) => newState

It is known as the reducer because they are the type of function that would pass to Array.prototype.reduce(reducer, ?initialValue). It is very essential to ensure that the reducer stays pure.

To maintain this, there are few things that you should never do inside the reducer:

  • Modify its argument
  • Make sure not to perform some side effects such as routing transitions and API calls
  • Call non-pure functions, e.g. Date.now() or Math.random().

Example

const initialState = { value: 0 }function counterReducer(state = initialState, action) {
// Check to see if the reducer cares about this action
if (action.type === 'counter/incremented') {
// If so, make a copy of `state`
return {
...state,
// and update the copy with the new value
value: state.value + 1
}
}
// otherwise return the existing state unchanged
return state
}

10. What is the purpose of the constants in Redux?

When you use an IDE, the constants allow you to find all the usages of specific functionality across the project. It also prevents you from making silly mistakes which are usually caused by typos; in that case, you will receive a Reference Error immediately.

11. What is Redux Thunk?

Redux Thunk middleware is something that allows the developers to write the action creators that return functions, not actions. The redux-thunk can also be used for postponing the dispatch of action or to dispatch just if a specific condition is met. The inner function gets the “store” methods dispatch and getState() as the parameters.

Originally published at https://www.bestinterviewquestion.com.

Full Stack developer from India having 7+ year experience https://www.bestinterviewquestion.com