Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How is Relay different from Redux?

Answer»
RelayRedux
It only manages the state that ORIGINATED from the server.Redux manages and handles all the states of the application.
Relay CACHES and optimizes the data.Redux does not HANDLE data fetching; however, this can be done MANUALLY.
2.

What is Redux Thunk?

Answer»

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.

3.

What is the purpose of the constants in Redux?

Answer»

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.

4.

What are reducers in redux?

Answer»

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
}

5.

What are the downsides of Redux compared to Flux?

Answer»

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.
6.

How to structure Redux top-level directories?

Answer»

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.
7.

How to access redux stores outside a react component?

Answer»

To access the REDUX stores outside a react COMPONENT, you need to export the store from the MODULE where it has been created with createStore.

NOTE: If you are looking for React Native INTERVIEW Questions then you can visit here.

Example

store = createStore(myReducer);
export default store;

8.

What is the difference between React context and React redux?

Answer»
REACT ContextReact Redux
This can be used in the application directly and best for passing the DATA to the DEEPLY nested components.To use this in the application, you need to code it separately and then need to MERGE them.
Context API doesn’t provide a large number of features.Redux is much more powerful and provides a large number of features
9.

How to add multiple middlewares to Redux?

Answer»

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);

10.

What is “store” in redux?

Answer»

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'])

function addTodo(text) {
  return {
    type: 'ADD_TODO',
    text
  }
}

store.dispatch(addTodo('Read the docs'))
store.dispatch(addTodo('Read about the middleware'))

11.

What is an action in Redux?

Answer»

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.

Also READ: Next js Interview QuestionsExample

const addTodoAction = {
      type: 'ADD',
      payload: 'Buy-milk'
}

12.

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

Answer»

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.

Major features of Redux DevTools are:
  • It allows you to inspect all the states and action payload.
  • It allows you to go back into the time simply by canceling the actions.
  • Each stage action will be re-evaluated in case you change the reducer code.
  • With the help of persistState() store enhancer, you can continue your debug sessions across page reloads.

If you want to READ more about this TOPIC in detail then you can visit here.

13.

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

Answer»

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.

14.

What is the difference between mapStateToProps() and mapDispatchToProps()?

Answer»
mapStateToProps()mapDispatchToProps()
It is a FUNCTION that is used to provide the stored data to the component.It is a function that is used to provide the ACTION creators with props to the component.
All the results of mapStateToProps() should be the PLAIN object that will later be merged into the component’s prop.By mapDispatchToProps(), all the action creators are wrapped in the dispatcher call so that they may be called UPON directly and will be merged into the component’s prop.
It is used to connect the REDUX state to the props of the react component.It is used to connect redux actions to the react props.
15.

What are the core principles of Redux?

Answer»

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.

NOTE: If you WANT to learn more about Redux then you can visit here.