React Interview
React Interview
React Interview
reactjs angular
------ ---------
1. it is a library 1. it is a framework.(frameWork
means which provides complete developement solution
like design,databinding,get data from
server,routing,state Management)
both are not good in state management so we use redux it is developed for js
applications
what is react?
React is a open source frontend js library for building user interfaces based on
components and we have more developer community,it is developed
by facebook developer.
version:18.2.0
why react and why not other frameworks?
1.React’s key features include the ability to use third-party libraries.
2.componental architecure where we can reuse the code modules wherever we
want,lesscode,faster developement,
3.virtual dom(it is a blue print of original dom).(Virtual DOM – anytime the DOM
changes, a new virtual DOM is created, compared to the
previous one, and only the differences are modified in the “real” DOM.)
4.One-directional data binding provides code stability.(One-way data binding means
that a UI element can’t affect a component’s state.)
5.easy to learn.
6.React is comparatively faster than other frameworks. There is a reusability
factor that helps in saving time and money in development.
It supports server-side rendering of websites making it faster by all means
3.what is jsx?
jsx is a extension of java script lnaguage it looks like html but not ,where it is
introduced to overcome the writing of react syntaxes where
the react will convert this jsx into normal javascript language by using babel
(where it converts the jsx to javascript);
element:
=======
1.An element is a plain object describing a component instance or DOM node and its
desired properties. It contains only information
about the component type (for example, a Button), its properties (for example, its
color), and any child elements inside it.
2.
In simple words, If the previous value of state or props and the new value of state
or props is the same, the component will not re-render
itself. Since Pure Components restricts the re-rendering when there is no use of
re-rendering of the component. Pure Components are
Class Components which extends React.PureComponent.
It is the type of component which re-renders only when the props passed to it
changes and not even if its parent component re-renders or
if the shouldComponentUpdate()method is called. It is greatly used to enhance the
performance of a web application.
In the following example, we are going to use the PureComponent with Comp1
component so that it only re-renders if the props passed to it
changes.
if nested data structures are present in state or props purecomponent will not
work.
eg:
The syntactic representation of memoized components looks like below,
const MemoizedComponent = memo(SomeComponent, arePropsEqual?);
In the above code, the email prop has not been passed to child component. So there
won't be any re-renders for email prop change.
Unlike props, state can be modified by the component and is used to manage the
internal state of the component. Changes in the state
trigger a re-render of the component and its children. props (short for
"properties") are passed to a component by its parent component and are
read-only, meaning that they cannot be modified by the component itself. props can
be used to configure the behavior of a component and to pass
data between components.
//Wrong
this.state.message = "Hello world";
Instead use setState() method. It schedules an update to a component's state
object. When state changes, the component responds by re-rendering.
//Correct
this.setState({ message: "Hello World" });
Note: You can directly assign to the state object either in constructor or using
latest javascript's class field declaration syntax.
Public class fields syntax: If you don't like to use bind approach then public
class fields syntax can be used to correctly bind callbacks.
The Create React App eanables this syntax by default.
handleClick = () => {
console.log("SingOut triggered", this);
};
<button onClick={this.handleClick}>SingOut</button>
Arrow functions in callbacks: It is possible to use arrow functions directly in the
callbacks.
handleClick() {
console.log('SingOut triggered');
}
render() {
return <button onClick={() => this.handleClick()}>SignOut</button>;
}
Note: If the callback is passed as prop to child components, those components might
do an extra re-rendering. In those cases, it is preferred to go with .bind() or
public class fields syntax approach considering performance.
9.What is "key" prop and what is the benefit of using it in arrays of elements?
A key is a special attribute you should include when creating arrays of elements.
Key prop helps React identify which items have changed,
are added, or are removed.
Keys should be unique among its siblings. Most often we use ID from our data as
key:
Using indexes for keys is not recommended if the order of items may change. This
can negatively impact performance and may cause issues with component state.
If you extract list item as separate component then apply keys on list component
instead of li tag.
There will be a warning message in the console if the key prop is not present on
list items.
The key attribute accepts either string or number and internally convert it as
string type.
This is a recently added approach. Refs are created using React.createRef() method
and attached to React elements via the ref attribute. In order to use refs
throughout the component, just assign the ref to the instance property within
constructor.