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 can the addition of multiple middlewares to Redux be done?

Answer»

In ORDER to add multiple middlewares to Redux, the usage of APPLYMIDDLEWARE can do the work. In applyMiddleware, we can pass every piece of middleware as a new argument. Therefore, our job is to just pass each piece of middleware that we want. In the example given below, we have added Redux Thunk and logger middlewares as an argument:

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

The purpose of this article was to explain to our readers what Redux actually is, what is it used for and what are the most important questions asked in the interviews. It is one of the most important JavaScript libraries which is being used alongside REACT for building a number of applications seamlessly and therefore, we would suggest that any budding talent of today should be familiar with the concepts of Redux to get an edge over others. 

Given below are some of the REFERENCES and recommended resources to learn more about Redux:

2.

What are the things which we should never do inside a reducer?

Answer»

The things which we should never do inside a reducer are as follows:

  • MODIFY the argument of the reducer
  • We should assure that we do not perform any SIDE operations such as routing TRANSITIONS, API calls, etc.
  • We should not CALL non-pure functions, for instance Date.now(), Math.random(), etc.
3.

What do you understand about the Redux Saga?

Answer»

Redux Saga is a middleware LIBRARY that can be useful for allowing a Redux store to interact with the resources outside of itself in an asynchronous manner, for example, making HTTP REQUESTS to external services, accessing browser storage, executing Input/Output operations and many more. These operations are also called side effects.

4.

Differentiate between React Redux and React's Context API.

Answer»

The KEY differences in the comparison: Context Api VS Redux are as follows:

React ReduxReact Context API
In order to use React-Redux in our APPLICATION, we need to code it separately and then merge it into the main project.React Context can be used in the application directly.
The number of features provided by React-Redux is COMPARATIVELY more than React Context.The number of features provided by React Context is comparatively less than React-Redux.
5.

How can we access a redux store outside a react component?

Answer»

For accessing a redux STORE outside a REACT COMPONENT, we can export the store from the module where it has been CREATED with createStore as done in the following EXAMPLE:

store = createStore(reducer);export default store;
6.

How can we structure the top level directories in Redux?

Answer»

EVERY Redux application has multiple top-level directories as GIVEN below:

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

What are Redux forms? What are its major features?

Answer»

Redux Forms PRESENT in React and Redux help in enabling a form in React to use Redux for STORING all of its states. They can be used with raw inputs in HTML5. Redux forms WORK extremely well with User Interface (UI) frameworks, for instance, Material UI, React Widgets, React Bootstrap and many more.

The major FEATURES of Redux forms are as follows:

  • Field values persistence through the Redux store.
  • Validation (synchronous/asynchronous) and submission.
  • Formatting, parsing and normalization of field values.
8.

How can we use connect from react redux?

Answer»

In order to use connect from React REDUX, we will have to follow a couple of steps to use our store in our container:

  • Firstly, we use the mapStateToProps(): This would map the state VARIABLES from our store to the props which we specify.
  • Secondly, we connect the above props to our container: The OBJECT returned by the mapStateToProps component is connected to the container.

A sample code for connect from react-redux is GIVEN below:

import React from 'react';import { connect } from 'react-redux'; class App extends React.Component { render() { return <div&GT;{this.props.containerData}</div>; } } function mapStateToProps(state) { return { containerData: state.appData }; } export default connect(mapStateToProps)(App);function mapStateToProps(state) { return { containerData: state.data };}export default connect(mapStateToProps)(App);
9.

Differentiate between Relay and Redux.

Answer»

The key differences between RELAY and Redux are given below:

RelayRedux
The state originating from the server is taken care of by Relay.All the STATES of the APPLICATION are taken care of by Redux.
Relay can be used for caching and optimizing the data.Redux is not responsible for handling data FETCHING (it can be DONE manually though).
10.

What are the workflow features in Redux?

Answer»

The workflow features in Redux are as follows:

  • RESET: The state of the STORE is allowed to be reset.
  • Revert: Revert or Rollback to the last committed state is allowed.
  • Sweep: Every disabled action which we have fired unintentionally will be removed.
  • Commit: The current state is MADE the INITIAL state.
11.

What do you understand about Redux Thunk?

Answer»

USING Redux Thunk middleware, we can write action creators returning a function instead of an action. This thunk can postpone the dispatch of an action, or do conditional dispatchment. The arguments passed to the inner function are the store methods dispatch and getState(). In the event of an action creator returning a function, the function gets executed by the Redux Thunk middleware and it does not have to be pure. In other words, the function is allowed to have side EFFECTS, including executing asynchronous API CALLS. It can even dispatch actions. Redux thunk is used to delay the dispatch of an action, or to dispatch in the event of a CERTAIN condition being met. At the time of dispatch of a function instead of an action object, if Redux Thunk middleware is enabled, the middleware will call that function with the dispatch method itself as the first argument.

12.

Explain with an example how to set the initial state in Redux.

Answer»

In ORDER to set the initial state in Redux, we have to pass the initial state as the SECOND argument to createStore as shown below:

 const rootReducer = combineReducers({ TODOS: todos, visibilityFilter: visibilityFilter});const initialState = { todos: [{id:100, name:'ritik', COMPLETED: true}]};const store = createStore( rootReducer, initialState);
13.

Give an example depicting the usage of a Redux store.

Answer»

An example depicting the usage of a REDUX store is given below:

 IMPORT { createStore } from 'redux'const store = createStore(TODOS, ['Use Redux'])FUNCTION deletingTodo(text) { return { type: 'DELETING_TODO', text }}store.dispatch(deletingTodo('Do the homework'))store.dispatch(deletingTodo('Buy coffee'))
14.

Name all the Redux Store methods.

Answer»

All the REDUX Store METHODS are as FOLLOWS:

  • getState()
  • subscribe(LISTENER)
  • dispatch(ACTION)
  • replaceReducer(nextReducer)
15.

Describe what is meant by a "store" in Redux.

Answer»

“Store” in Redux is used to carry TOGETHER all the states, REDUCERS, and actions which CREATE the app. Some of the responsibilities of the store are as FOLLOWS:

  • The state of the current application from inside is held by the Redux Store.
  • We can access the current state using store.getState().
  • We can UPDATE the state using store.dispatch(action).
  • We can also register listener callbacks using the store.subscriber(listener).
16.

What is the mental model of redux saga?

Answer»

REDUX Saga functions as a separate thread in our programme which is solely responsible for side effects. Redux Saga is a redux middleware. In other words, it means that it can be started, paused, and aborted from the main application using STANDARD Redux actions, has access to the ENTIRE Redux application state, and can ALSO dispatch Redux actions.

17.

Explain the typical data flow in an application made using React and Redux (Redux Lifecycle for an application).

Answer»

The typical data flow in Redux starts with a call back from the User Interface COMPONENT which dispatches an ACTION with a payload. After that, the reducers INTERCEPT and receive the DISPATCHED actions, generating a new application state. After that, the actions are propagated down through a hierarchy of COMPONENTS from the Redux store.