ARAbdur Rakib
All posts
mediumNov 3, 20222 min read

Redux Interview Questions

What is Redux? Redux is an open-source JavaScript library for managing and centralizing application states. It is most commonly used with libraries such as…

What is Redux? Redux is an open-source JavaScript library for managing and centralizing application states. It is most commonly used with libraries such as React or Angular for building user interfaces. Similar to Facebook’s Flux architecture, it was created by Dan Abramov and Andrew Clark.

How does redux work?Redux has mainly three building parts. i) Action: Action is a plain javascript object which contains a mandatory field type and a minor field payload.ii) Store: Store is a global object which holds the whole state of our applicationiii)Reducer: Reducer is a function that takes an action and previous state and returns a new state.Dispatch: Dispatch is a redux function. If any event occurs in view then redux dispatches an action with type and payload which is sent to the reducer and the reducer change state depending on the action.State update mechanism: Basically, in any application, we have three things such as view, action, and state. In view, if any event occurred like a click then it dispatches an action with type and payload. Then reducer listens to the action and changes the state depending on the action type and after changing the state View re-renders.

What is redux-thunk?Redux Thunk is a middleware that lets us call action creators that return a function instead of just actions (In this function we can call API and change our redux state depending on the response) instead of an action object. This middleware is used for handling asynchronous operations (delayed actions, including working with promises).

What is redux enhancer?Redux enhancers are high-order functions that take createStore as input and return a new enhanced version of createStore.

<a href="https://medium.com/media/d2d44a2adf6fa92d763bbdd1ad190446/href">https://medium.com/media/d2d44a2adf6fa92d763bbdd1ad190446/href</a>

How does redux persist work?Redux persist is a library allowing us to save the redux store in the local storage of your browser/AsyncStorage for react native. When our app launches, redux sets an initial state. Shortly after this, redux persist retrieves our persisted state from storage. Our persisted state then overrides any initial state.

Redux vs React Context, When should we use what?We should use context for those data which don’t change frequently like theme data, localization data e.t.c. Because if any state changes in a context then all consumers of this context will re-render, whether a consumer’s consumed value changed or not, the consumer will re-render.And we should use redux when, - our application is very big and contains complex states- we want to have greater control over states and actions in our applications.- we want middleware for various purposes (logging actions, error reporting, dispatching other requests depending on the server’s response, etc.)- we want to persist our state.- and if we don’t want server response to directly change the state of our application.

Useful Links: - https://www.shawndsilva.com/blog/web-development/differences-between-redux-and-redux-toolkit-and-why-should-you-upgrade- https://leewarrick.com/blog/the-problem-with-context/- https://stackoverflow.com/questions/49568073/react-context-vs-react-redux-when-should-i-use-each-one

Thanks for reading. Any comments will be appreciated. Stay safe & happy, bye.

reduxreactredux-toolkitcontext-api