React 1
React 1
->Virtual DOM is a simple Javascript object that is the exact lightweight copy of real DOM.
-> It can be consider as NODE tree which have elements, attributes and other properties.
How it helps->
Here’s what happens when we try to update the DOM in React:
1. The entire virtual DOM gets updated.
2. The virtual DOM gets compared to what it looked like before you updated it. React figures out
which objects have changed.
3. The changed objects, and the changed objects only, get updated on the real DOM.
4. Changes on the real DOM cause the screen to change.
5) What is JSX?
No. Browser can read javascript object. So for making JSX browser readable it has to be transformed in
to Java script object.
It is a UI library only. As such when building something with React we will have to include other
libraries to handle other parts of an application.
No predefined way to structure your app (such as services, controllers & views in Angular).
A higher-order component (HOC) is an advanced technique in React for reusing component logic
It is actually a function that takes one component and returns another component that wraps the original
one.
The lifecycle of the component is divided into four phases. They are:
1. Initial Phase
2. Mounting Phase
3. Updating Phase
4. Unmounting Phase
1. Initial Phase-
2. Mounting Phase
In this phase, the instance of a component is created and inserted into the DOM. It consists of the
following methods-
componentWillMount()-
This is invoked immediately before a component gets rendered into the DOM. In the case, when you
call setState() inside this method, the component will not re-render.
ComponentDidMount()-
This is invoked immediately after a component gets rendered and placed on the DOM. Now, you can
do any DOM querying operations.
Render()
This method is defined in each and every component. It is responsible for returning a single root
HTML node element. If you don't want to render anything, you can return a null or false value.
3) Updating Phase-
ComponentWillRecieveProps()
It is invoked when a component receives new props. If we want to update the state in response to prop
changes, we should compare this.props and nextProps to perform state transition by using
this.setState() method.
shouldComponentUpdate()
It is invoked just before the component updating occurs. Here, you can't change the component state by
invoking this.setState() method. It will not be called, if shouldComponentUpdate() returns false.
render()
It is invoked to examine this.props and this.state and return one of the following types: React elements,
Arrays and fragments, Booleans or null, String and Number. If shouldComponentUpdate() returns
false, the code inside render() will be invoked again to ensure that the component displays itself
properly.
componentDidUpdate()
It is invoked immediately after the component updating occurs. In this method, you can put any code
inside this which you want to execute once the updating occurs. This method is not invoked for the
initial render.
4. Unmounting Phase
It is the final phase of the react component lifecycle. It is called when a component instance is
destroyed and unmounted from the DOM. This phase contains only one method and is given below.
componentWillUnmount()
This method is invoked immediately before a component is destroyed and unmounted permanently.
It performs any necessary cleanup related task such as invalidating timers, event listener, canceling
network requests, or cleaning up DOM elements.
If a component instance is unmounted, you cannot mount it again.
Source- https://www.javatpoint.com/react-component-life-cycle
Each view is considered as a new file in convectional routing while in react it is considered as a single
HTML entity.
ON Navigation whole view got refreshed in conventional routing while in react routing view will not
be refreshed only objects will refresh.
Yes any Ajax library like axios, Jquery Ajax can be used with react.
Stateful components are the entities that stores the changes that happen and place them in memory.
It can contains the state object and event handling function, user actions as well.
Refs provide a way to access DOM nodes or React elements created in the render method.
The controlled component is a way that we can handle the form input
value using the state.
To change the input value there is only one way to change it is using
setState or useState if you are using React Hook
Route: Route is the conditionally shown component that renders some UI when its path matches the
current URL.
Link: Link component is used to create links to different routes and implement navigation around the
application. It works like HTML anchor tag.
Switch: Switch component is used to render only the first route that matches the location rather than
rendering all matching routes. Although there is no defying functionality of SWITCH tag in our
application because none of the LINK paths are ever going to coincide. But let’s say we have a route
(Note that there is no EXACT in here), then all the Route tags are going to be processed which start
with ‘/’ (all Routes start with /). This is where we need SWITCH statement to process only one of the
statements.
a. The function always returns the same result if the same arguments are passed in. It does not
depend on any state, or data, change during a program’s execution. It must only depend on its input
arguments.
b. The function does not produce any observable side effects such as network requests, input and
output devices, or data mutation.
Same Input => Same Output
add(2, 4); // 6
The example returns a value based on the given parameters, regardless of where/when you call it.
let x = 2;
Pure Components in React are the components which do not re-renders when the value of state and
props has been updated with the same values.
If the value of the previous state or props and the new state or props is the same, the component is not
re-rendered.
Pure Components restricts the re-rendering ensuring the higher performance of the Component.
Pure Components are more performant in certain cases.
setInterval(() => {
this.setState({
counter: 0
});
}, 1000);
}
render() {
// This function wont be re-rendered in case when the new state is same as previous
Keys are used to React to identify which items in the list are changed, updated, or deleted.
In other words, we can say that keys are used to give an identity to the elements in the lists.
When we set React in production, warnings and other development features are not shown.
function ExampleApplication() {
return (
<div>
<Header />
<React.StrictMode> <div>
<ComponentOne />
<ComponentTwo />
</div>
</React.StrictMode> <Footer />
</div>
);
}
React.memo(): This is used to prevent all of the unnecessary rendering in functional component.
PureComponent: This is used to prevent all of the unnecessary rendering in class component.
}
}
The reason why this cannot be allowed before super() is because this is uninitialized if super() is not
called. However even if we are not using this we need a super() inside a constructor because ES6 class
constructors MUST call super if they are subclasses. Thus, you have to call super() as long as you have
a constructor. (But a subclass does not have to have a constructor).
We call super(props) inside the constructor if we have to use this.props, for example:
}
}
There is only one reason when one needs to pass props to super():
Passing:
class MyComponent extends React.Component {
constructor(props) {
super(props)
console.log(this.props)
// -> { icon: 'home', … }
}
}
Not passing:
console.log(this.props)
// -> undefined
render() {
// No difference outside constructor
console.log(this.props)
// -> { icon: 'home', … }
}
}
We can use the propType for validating any data we are receiving from props
React.PropTypes.bool
React.PropTypes.func
React.PropTypes.node
React.PropTypes.number
React.PropTypes.string
React Hooks are functions that let us hook into the React state and lifecycle features from function
components. By this, we mean that hooks allow us to easily manipulate the state of our functional
component without needing to convert them into class components