-
What is the use of the
ownPropsparameter inmapStateToProps()andmapDispatchToProps()?If the
ownPropsparameter is specified, React Redux will pass the props that were passed to the component into your connect functions. So, if you use a connected component:import ConnectedComponent from './containers/ConnectedComponent'; <ConnectedComponent user={'john'} />The
ownPropsinside yourmapStateToProps()andmapDispatchToProps()functions will be an object:{ user: 'john' }You can use this object to decide what to return from those functions.
-
How to structure Redux top level directories?
Most of the applications has several top-level directories as below:
- Components: Used for dumb components unaware of Redux.
- Containers: Used for smart components connected to Redux.
- Actions: Used for all action creators, where file names correspond to part of the app.
- Reducers: Used for all reducers, where files name correspond to state key.
- Store: Used for store initialization.
This structure works well for small and medium size apps.
-
What is redux-saga?
redux-sagais a library that aims to make side effects (asynchronous things like data fetching and impure things like accessing the browser cache) in React/Redux applications easier and better.It is available in NPM:
$ npm install --save redux-saga -
What is the mental model of redux-saga?
Saga is like a separate thread in your application, that's solely responsible for side effects.
redux-sagais a redux middleware, which means this thread can be started, paused and cancelled from the main application with normal Redux actions, it has access to the full Redux application state and it can dispatch Redux actions as well. -
What are the differences between
call()andput()in redux-saga?Both
call()andput()are effect creator functions.call()function is used to create effect description, which instructs middleware to call the promise.put()function creates an effect, which instructs middleware to dispatch an action to the store.Let's take example of how these effects work for fetching particular user data.
function* fetchUserSaga(action) { // `call` function accepts rest arguments, which will be passed to `api.fetchUser` function. // Instructing middleware to call promise, it resolved value will be assigned to `userData` variable const userData = yield call(api.fetchUser, action.userId) // Instructing middleware to dispatch corresponding action. yield put({ type: 'FETCH_USER_SUCCESS', userData }) } -
What is Redux Thunk?
Redux Thunk middleware allows you to write action creators that return a function instead of an action. The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met. The inner function receives the store methods
dispatch()andgetState()as parameters. -
What are the differences between
redux-sagaandredux-thunk?Both Redux Thunk and Redux Saga take care of dealing with side effects. In most of the scenarios, Thunk uses Promises to deal with them, whereas Saga uses Generators. Thunk is simple to use and Promises are familiar to many developers, Sagas/Generators are more powerful but you will need to learn them. But both middleware can coexist, so you can start with Thunks and introduce Sagas when/if you need them.
-
What is Redux DevTools?
Redux DevTools is a live-editing time travel environment for Redux with hot reloading, action replay, and customizable UI. If you don't want to bother with installing Redux DevTools and integrating it into your project, consider using Redux DevTools Extension for Chrome and Firefox.
-
What are the features of Redux DevTools?
Some of the main features of Redux DevTools are below,
-
Lets you inspect every state and action payload.
-
Lets you go back in time by cancelling actions.
-
If you change the reducer code, each staged action will be re-evaluated.
-
If the reducers throw, you will see during which action this happened, and what the error was.
-
With
persistState()store enhancer, you can persist debug sessions across page reloads.
-
-
What are Redux selectors and why to use them?
Selectors are functions that take Redux state as an argument and return some data to pass to the component.
For example, to get user details from the state:
const getUserData = state => state.user.dataThese selectors have two main benefits,
-
The selector can compute derived data, allowing Redux to store the minimal possible state
-
The selector is not recomputed unless one of its arguments changes
-