React_js
React_js
React Js is modern library for Javascript. Used to make a single page applications. It is
following new ES6 features. So that it’s very fast and developer friendly when compared to
other Javascript libraries.
JSX:
Known as Javascript XML, we can write Javascript and HTML in single line need to
separate it.
It’s a shorter syntax for html and Javascript code.
Babel:
Babel is a Javascript compiler
Used to convert JSX code into actual Javascript code for the browser understanding.
Webpack:
Webpack is a module bundler
It takes whole CSS, Images, and other Javascript files into final bundle, that will be
run in the browser.
In real time functional components are commonly used due to simplicity, and
performance.
5. What is the virtual DOM and how does React use it?
Virtual Dom is light weight memory always having the copy of actual DOM.
So whenever we change or modify something Virtual DOM is render for that
particular part and update into actual DOM. Not Loaded the entire app for every
render.
Props:
Props are Properties, used to pass the data from parent to child components.
It’s Immutable
State:
State is used to store and manage data in components.
Set initially for creating component and that will be updated after changing /
modifying in the component.
It’s mutable.
Prop Drilling occurs when we pass the same prop data from the parent component to
multiple child components, but only few child components will use this data. In this case,
hard to manage. To avoid this, we use useContext hook from Context Api method.
10. What is the difference between Virtual DOM, and Real DOM in React JS?
Virtual DOM:
Having the copy of actual DOM.
It loads only changed or modified part of component.
So that very fast performance.
It compares always previous execution of DOM and current execution of DOM. (ie.,
Reconciliation)
After updates react unmount automatically by using inbuilt life cycle methods. So It
avoids memory wastage.
Real DOM:
Directly updates into actual DOM
It loads the entire app for every changes.
Slow performance.
Controlled Components update the states via hooks or this. state which means
whenever we change or modify data in form inputs, rendered for each updates.
Uncontrolled Components updates input data into DOM directly. Here no re-render
is available.
Lifting State Up used to avoid prop drilling and share the same state to the multiple
child components.
But Its used for local, Parent to Child components. Not for globally declaration.
13. What are Hooks in React JS? and Explain useEffect, useState, useMemo, and
useCallback hooks in detail.
useEffect
It is similar to componentDidMount () and componentDidUpdate () in life cycle
methods of class component.
It makes side effect for the components after rendering.
Mostly used for API calls, Timers, and subscriptions in real time functionalities.
useState
It is used to update store the initial value and update the state
Example: Count increment
useMemo
useMemo is used to memo zing the result value of the function.
It avoids the re-calculations in a component, so that re-rendering should be avoided
when the state is no changed,
Example: Searching a product list, if we have no change in searching input terms,
Virtual Dom compares the previous result and current result, so that return the same
result because it memo zed (memorised) the result.
useCallback
useCallback is also memo zing the function.
It avoids the re-creation of function.
It takes the reference of the previous function memory and when we change and
update it uses that reference not recreating.
And functions passed as a props to the child components. [ States are not updated
in child components directly so that we use call-backs the entire function for
updating]
Form inputs are handled by controlled components, when we change or entering the values
in form elements, render the component and updates the state vales.
Virtual DOM always compares the previous DOM execution and current changes so that
only executes needed part of component. So that reconciliation helps to load the page very
fast.
Both are used to make the side effects for the component.
useEffect
It is efficient way of updating state
It’s enough to handle multiple updating depends on use case. Like
componentDidMount, componentDidUpdate, componentWillUnmount
Used in functional components
componentDidMount
It’s less efficient
Only used in mounting.
We can handle the events in react by using camel case syntax and JSX code.
Example: onClick, onSubmit, etc.,
19. What are keys in React lists, and why are they important?
Keys are used to identifying the item in a list of array elements in react.
When we add or removing the item its exactly focusing that particular item.
Index also do the same logic but when we add or removing something the index
position will be change so that mismatch will be happen.
defaultProps
Used to set the default prop value to the parent component when no passed the
props.
propTypes
Used to declare the datatypes to the props data. So that we can identify and solve
the bugs easily.
Portal is used to run the child component outside of the parent hierarchy. But still
connected with react components.
Example: HTML file
<div id =” root” ></div>
<div id =” root-product” ></div>
NPM/Yarn Node Package Manager used to install the packages from react library &
Start using yarn start or npm start for staring the application
package. Json It contains the installed packages inside the script in this file.
25. Basic Testing with tools like Jest or React Testing Library?
Jest is one of the react testing library, which is used to developer test the
components in efficient way instead of using manual testing.
There is no proof for manual testing, if the developer tested the component or not,
but it has the proof.
Used to find out the errors, debugging is easy and get cleaned code.
26. What is Redux? What are Reducer, Action, and Store in Redux?
Middleware is sit between the action and reducer, and handles the asynchronous
actions.
In real time, we need to handle login, Api calls, notifications likewise async actions.
Because Redux is only suitable for synchronous actions.
Redux is following the single direction, ie., Unidirectional Data flow (One-way Data
Binding)
React UI
→ dispatch(action)
→ middleware (e.g., thunk/logger)
→ reducer updates state
→ store updates
→ UI re-renders