Skip to content

Commit 0147e87

Browse files
committed
Add Redux middleware and tools questions
1 parent f470034 commit 0147e87

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

README.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,16 @@ Below is a list of ReactJS interview questions and answers.
157157
|149| [What are the different ways to write mapDispatchToProps?](#what-are-the-different-ways-to-write-mapDispatchToProps)|
158158
|150| [What is the use of the ownprops arg in mapStateToProps and mapDispatchToProps?](#what-is-the-use-of-the-ownprops-arg-in-mapstatetoprops-and-mapdispatchtoprops)|
159159
|151| [How to structure redux top level directories?](#how-to-structure-redux-top-level-directories)|
160-
160+
|152| [What is redux saga?](#what-is-redux-saga?)|
161+
|153| [What is the mental model of redux saga?](#what-is-the-mental-model-of-redux-saga)|
162+
|154| [What are the differences between call and put in redux-saga](#what-are-the-differences-between-call-and-put-in-redux-saga)|
163+
|155| [What is Redux thunk?](#what-is-redux-thunk)|
164+
|156| [What are the differences between redux-saga and redux thunk](#what-are-the-differences-between-redux-saga-and-redux-thunk)|
165+
|157| [What is React Dev Tools?](#what-is-react-dev-tools)|
166+
|158| [Why React tab is not showing up for dev tools?](#why-react-tab-is-not-showing-up-for-dev-tools)|
167+
|159| [What is Redux DevTools?](#what-is-redux-devtools)|
168+
|160| [What are the features of Redux DevTools?](#what-are-the-features-of-redux-devtools)|
169+
|161| [How to focus an input element on page load?](#how-to-focus-an-input-element-on-page-load)|
161170

162171
### What is ReactJS?
163172

@@ -1558,6 +1567,10 @@ Redux follows three fundamental principles
15581567

15591568
### What are Redux selectors?Why use them?
15601569
Selectors are functions that take Redux state as an argument and return some data to pass to the component.
1570+
For example, to get user details from the state,
1571+
```
1572+
const getUserData = state => state.user.data;
1573+
```
15611574

15621575
### What is reselect?How it works?
15631576
Reselect is a selector library(for Redux) which uses memoization concept. It was originally written to compute derived data from redux-like applications state, but it cann't be tied to any architecture or library.
@@ -2355,3 +2368,83 @@ Most of the applications has several top-level directories as below
23552368
5. **Store** Used for store initialization
23562369
This structure works well for small and mid-level size apps.
23572370

2371+
### What is redux-saga?
2372+
redux-saga is a library that aims to make side effects(i.e. asynchronous things like data fetching and impure things like accessing the browser cache) in React/Redux applications easier and better.
2373+
It is available in NPM as
2374+
```
2375+
npm install --save redux-saga
2376+
```
2377+
### What is the mental model of redux-saga?
2378+
The mental model is that a saga is like a separate thread in your application that's solely responsible for side effects. redux-saga is 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.
2379+
### What are the differences between call and put in redux-saga?
2380+
Both **call** and **put** are effects creators functions. call function is used to create effect description, which instructs middleware to call the promise. put function creates effect, in which instructs middleware to dispatch an action to the store.
2381+
Let's take example of how these effects work for fetching particular user data
2382+
```
2383+
function* fetchUserSaga(action) {
2384+
// `call` function accepts rest arguments, which will be passed to `api.fetchUser` function.
2385+
// Instructing middleware to call promise, it resolved value will be assigned to `userData` variable
2386+
const userData = yield call(api.fetchUser, action.userId);
2387+
// Instructing middleware to dispatch corresponding action.
2388+
yield put({
2389+
type: 'FETCH_USER_SUCCESS',
2390+
userData
2391+
});
2392+
}
2393+
```
2394+
### What is Redux thunk?
2395+
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 and getState() as parameters.
2396+
### What are the differences between redux-saga and redux-thunk?
2397+
Both Redux Thunk and Redux Saga take care of dealing with side effects. In most of the scenarios, Thunk allows Promises" to deal with them, whereas Saga uses Generators. Thunk is simple to use and Promises are familiar to many developers, Saga/Generators are more powerful but you will need to learn them. But both the two middleware can coexists, so you can start with Thunks and introduce Sagas when/if you need them.
2398+
### What is React Dev Tools?
2399+
React Developer Tools makes you inspect the React component hierarchy, including component props and state. It exists both as a browser extension (for Chrome and Firefox), and as a standalone app (works with other environments including Safari, IE, and React Native).
2400+
The official extensions available for different browsers or environments.
2401+
1. **Chrome extension**
2402+
2. **Firefox extension**
2403+
3. **Standalone app (Safari, React Native, etc)**
2404+
After installation the quick navigation icon would appear as below,
2405+
![ScreenShot](images/devtoolsTab.png)
2406+
and creates React new tab as below,
2407+
![ScreenShot](images/devtoolsInspect.png)
2408+
### Why React tab is not showing up for dev tools?
2409+
When the page loads, the devtools sets a global named __REACT_DEVTOOLS_GLOBAL_HOOK__, then React communicates with that hook during initialization. If the website is not using React or if React fails to communicate with DevTools then it won't show up the tab.
2410+
### What is Redux DevTools?
2411+
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.
2412+
### What are the features of Redux DevTools?
2413+
Below are the major features of redux devTools
2414+
1. Lets you inspect every state and action payload
2415+
2. Lets you go back in time by “cancelling” actions
2416+
3. If you change the reducer code, each “staged” action will be re-evaluated
2417+
4. If the reducers throw, you will see during which action this happened, and what the error was
2418+
5. With persistState() store enhancer, you can persist debug sessions across page reloads
2419+
2420+
### How to focus an input element on page load?
2421+
You can do it in two steps
2422+
1. **Define ref callback for input**
2423+
```
2424+
class App extends React.Component{
2425+
componentDidMount(){
2426+
this.nameInput.focus();
2427+
}
2428+
render() {
2429+
return(
2430+
<div>
2431+
<input
2432+
defaultValue="Won't focus"
2433+
/>
2434+
<input
2435+
ref={(input) => { this.nameInput = input; }}
2436+
defaultValue="will focus"
2437+
/>
2438+
</div>
2439+
);
2440+
}
2441+
}
2442+
2443+
ReactDOM.render(<App />, document.getElementById('app'));
2444+
```
2445+
2. **Apply input focus in componentDidMount**
2446+
```
2447+
componentDidMount(){
2448+
this.nameInput.focus();
2449+
}
2450+
```

images/devtoolsInspect.png

145 KB
Loading

images/devtoolsTab.png

33 KB
Loading

0 commit comments

Comments
 (0)