React Js Interview Questions and Answers
React Js Interview Questions and Answers
1. What is React?
Ans. React is an open-source frontend JavaScript library which is used for building user interfaces especially for single page applications. It is used for handling view layer for web and mobile apps. React was created by Jordan Walke, a software engineer working for Facebook. React was first deployed on Facebook’s News Feed in 2011 and on Instagram in 2012.
2. What are the major features of React?
The major features of React are :
1. It uses VirtualDOM instead of RealDOM considering that RealDOM manipulations are expensive.
2. Supports server-side rendering.
3. Follows Unidirectional data flow or data binding.
4. Uses reusable/composable UI components to develop the view.
3. What is JSX?
A. JSX is a XML-like syntax extension to ECMAScript (the acronym stands for JavaScript XML). Basically it just provides syntactic sugar for the React.createElement()[ function, giving us expressiveness of JavaScript along with HTML like template syntax.In the example below text inside[php] tag is returned as JavaScript function to the render function.[/php]
4.What is the difference between Element and Component?
A. An Element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. Elements can contain other Elements in their props. Creating a React element is cheap. Once an element is created, it is never mutated.The object representation of React Element would be as follows:[php]const element = React.createElement(‘div’, { id: ‘login-btn’ }, ‘Login’);[/php]
The above React.createElement() function returns an object:
[php]{
type: ‘div’,
props: {
children: ‘Login’,
id: ‘login-btn’
}
}[/php]
And finally it renders to the DOM using ReactDOM.render():
[php]
[/php]
Whereas a component can be declared in several different ways. It can be a class with a render() method. Alternatively, in simple cases, it can be defined as a function. In either case, it takes props as an input, and returns a JSX tree as the output:
[php]const Button = ({ onLogin }) => (
);[/php]
Then JSX gets transpiled to a React.createElement() function tree:
[php]const Button = ({ onLogin }) =>
React.createElement(‘div’, { id: ‘login-btn’, onClick: onLogin }, ‘Login’);[/php]
5.How to create components in React?
A. There are two possible ways to create a component.Function Components: This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as first parameter and return React elements:[php]function Greeting({ message }) {
return
{`Hello, ${message}`}
;
}[/php]
Class Components: You can also use ES6 class to define a component. The above function component can be written as:
[php]class Greeting extends React.Component {
render() {
return
{`Hello, ${this.props.message}`}
;
}
}[/php]
6.When to use a Class Component over a Function Component?
A. If the component needs state or lifecycle methods then use class component otherwise use function component. However, from React 16.8 with the addition of Hooks, you could use state , lifecycle methods and other features that were only available in class component right in your function component.
7.What are Pure Components?
A. React.PureComponent is exactly the same as React.Component except that it handles the shouldComponentUpdate() method for you. When props or state changes, PureComponent will do a shallow comparison on both props and state. Component on the other hand won’t compare current props and state to next out of the box. Thus, the component will re-render by default whenever shouldComponentUpdate is called.
8.What is state in React?
A. State of a component is an object that holds some information that may change over the lifetime of the component. We should always try to make our state as simple as possible and minimize the number of stateful components.Let’s create an user component with message state,[php]class User extends React.Component {
constructor(props) {
super(props);
this.state = {
message: ‘Welcome to React world’,
};
}
render() {
return (
{this.state.message}
);
}
}[/php]
State is similar to props, but it is private and fully controlled by the component. i.e, It is not accessible to any component other than the one that owns and sets it.
9.What are props in React?
A. Props are inputs to components. They are single values or objects containing a set of values that are passed to components on creation using a naming convention similar to HTML-tag attributes. They are data passed down from a parent component to a child component.The primary purpose of props in React is to provide following component functionality:1.Pass custom data to your component.
2.Trigger state changes.
3.Use via this.props.reactProp inside component’s render() method.
For example, let us create an element with reactProp property:[php][/php]
This reactProp (or whatever you came up with) name then becomes a property attached to React’s native props object which originally already exists on all components created using React library.
[php]props.reactProp[/php]
10.What is the difference between state and props?
A. Both props and state are plain JavaScript objects. While both of them hold information that influences the output of render, they are different in their functionality with respect to component. Props get passed to the component similar to function parameters whereas state is managed within the component similar to variables declared within a function.
11.Why should we not update the state directly?
A. If you try to update state directly then it won’t re-render the component.[php]//Wrong
this.state.message = ‘Hello world’;[/php]
Instead use setState() method. It schedules an update to a component’s state object. When state changes, the component responds by re-rendering.
[php]//Correct
this.setState({ message: ‘Hello World’ });[/php]
Note: You can directly assign to the state object either in constructor or using latest javascript’s class field declaration syntax.
12.What is the purpose of callback function as an argument of setState()?
A. The callback function is invoked when setState finished and the component gets rendered. Since setState() is asynchronous the callback function is used for any post action.Note: It is recommended to use lifecycle method rather than this callback function.[php] () => console.log(‘The name has updated and component re-rendered’));[/php]
13.What is the difference between HTML and React event handling?
A. Below are some of the main differences between HTML and React event handling,1.In HTML, the event name should be in lowercase:[php][/php]
Whereas in React it follows camelCase convention:
[php]
1.In HTML, you can return false to prevent default behavior:
[php]href=”#” onclick=’console.log(“The link was clicked.”); return false;’ />[/php]
Whereas in React you must call preventDefault() explicitly:
[php]function handleClick(event) {
event.preventDefault();
console.log(‘The link was clicked.’);
}[/php]
1.In HTML, you need to invoke the function by appending () Whereas in react you should not append () with the function name. (refer “activateLasers” function in the first point for example)
14.How to bind methods or event handlers in JSX callbacks?
A. There are 3 possible ways to achieve this:1.Binding in Constructor: In JavaScript classes, the methods are not bound by default. The same thing applies for React event handlers defined as class methods. Normally we bind them in constructor.[php]class Component extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
// …
}
}[/php]
1.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.
[php]handleClick = () => {
console.log(‘this is:’, this);
};[/php][php][/php]
1.Arrow functions in callbacks: You can use arrow functions directly in the callbacks.
[php][/php]
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.
15.How to pass a parameter to an event handler or callback?
A. You can use an arrow function to wrap around an event handler and pass parameters:[php]
This is an equivalent to calling .bind:
[php][/php]
Apart from these two approaches, you can also pass arguments to a function which is defined as arrow function
[php];
handleClick = (id) => () => {
console.log(‘Hello, your ticket number is’, id);
};[/php]
16.What are synthetic events in React?
A. SyntheticEvent is a cross-browser wrapper around the browser’s native event. It’s API is same as the browser’s native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers
17.What are inline conditional expressions?
A. You can use either if statements or ternary expressions which are available from JS to conditionally render expressions. Apart from these approaches, you can also embed any expressions in JSX by wrapping them in curly braces and then followed by JS logical operator &&.[php]
Hello!
;
{
messages.length > 0 && !isLogin ? (
You have {messages.length} unread messages.
) : (
You don’t have unread messages.
);
}[/php]
18.What is “key” prop and what is the benefit of using it in arrays of elements?
A. A key is a special string attribute you should include when creating arrays of elements. Key prop helps React identify which items have changed, are added, or are removed.Most often we use ID from our data as key:[php]const todoItems = todos.map((todo) =>
- {todo.text}
);[/php]
When you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort:
[php] const todoItems = todos.map((todo, index) =>
- {todo.text}
);[/php]
Note:
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.
19.What is the use of refs?
A. The ref is used to return a reference to the element. They should be avoided in most cases, however, they can be useful when you need a direct access to the DOM element or an instance of a component.
20.How to create refs?
A. There are two approaches1.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.[php]class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return
;
}
} [/php]
1.You can also use ref callbacks approach regardless of React version. For example, the search bar component’s input element accessed as follows,
[php]class SearchBar extends Component {
constructor(props) {
super(props);
this.txtSearch = null;
this.state = { term: ” };
this.setInputSearchRef = (e) => {
this.txtSearch = e;
};
}
onInputChange(event) {
this.setState({ term: this.txtSearch.value });
}
render() {
return (
{this.state.term});
}
}[/php]
You can also use refs in function components using closures. Note: You can also use inline ref callbacks even though it is not a recommended approach
1. What is React?
Ans. React is an open-source frontend JavaScript library which is used for building user interfaces especially for single page applications. It is used for handling view layer for web and mobile apps. React was created by Jordan Walke, a software engineer working for Facebook. React was first deployed on Facebook’s News Feed in 2011 and on Instagram in 2012.
2. What are the major features of React?
The major features of React are :
1. It uses VirtualDOM instead of RealDOM considering that RealDOM manipulations are expensive.
2. Supports server-side rendering.
3. Follows Unidirectional data flow or data binding.
4. Uses reusable/composable UI components to develop the view.
3. What is JSX?
Ans. JSX is a XML-like syntax extension to ECMAScript (the acronym stands for JavaScript XML). Basically it just provides syntactic sugar for the React.createElement()[ function, giving us expressiveness of JavaScript along with HTML like template syntax.In the example below text inside[php] tag is returned as JavaScript function to the render function.[/php]
4.What is the difference between Element and Component?
Ans. An Element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. Elements can contain other Elements in their props. Creating a React element is cheap. Once an element is created, it is never mutated.The object representation of React Element would be as follows:[php]const element = React.createElement(‘div’, { id: ‘login-btn’ }, ‘Login’);[/php]
The above React.createElement() function returns an object:
[php]{
type: ‘div’,
props: {
children: ‘Login’,
id: ‘login-btn’
}
}[/php]
And finally it renders to the DOM using ReactDOM.render():
[php]
[/php]
Whereas a component can be declared in several different ways. It can be a class with a render() method. Alternatively, in simple cases, it can be defined as a function. In either case, it takes props as an input, and returns a JSX tree as the output:
[php]const Button = ({ onLogin }) => (
);[/php]
Then JSX gets transpiled to a React.createElement() function tree:
[php]const Button = ({ onLogin }) =>
React.createElement(‘div’, { id: ‘login-btn’, onClick: onLogin }, ‘Login’);[/php]
5.How to create components in React?
Ans. There are two possible ways to create a component.Function Components: This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as first parameter and return React elements:[php]function Greeting({ message }) {
return
{`Hello, ${message}`}
;
}[/php]
Class Components: You can also use ES6 class to define a component. The above function component can be written as:
[php]class Greeting extends React.Component {
render() {
return
{`Hello, ${this.props.message}`}
;
}
}[/php]
6.When to use a Class Component over a Function Component?
Ans. If the component needs state or lifecycle methods then use class component otherwise use function component. However, from React 16.8 with the addition of Hooks, you could use state , lifecycle methods and other features that were only available in class component right in your function component.
7.What are Pure Components?
Ans. React.PureComponent is exactly the same as React.Component except that it handles the shouldComponentUpdate() method for you. When props or state changes, PureComponent will do a shallow comparison on both props and state. Component on the other hand won’t compare current props and state to next out of the box. Thus, the component will re-render by default whenever shouldComponentUpdate is called.
8.What is state in React?
Ans. State of a component is an object that holds some information that may change over the lifetime of the component. We should always try to make our state as simple as possible and minimize the number of stateful components.Let’s create an user component with message state,[php]class User extends React.Component {
constructor(props) {
super(props);
this.state = {
message: ‘Welcome to React world’,
};
}
render() {
return (
{this.state.message}
);
}
}[/php]
State is similar to props, but it is private and fully controlled by the component. i.e, It is not accessible to any component other than the one that owns and sets it.
9.What are props in React?
Ans. Props are inputs to components. They are single values or objects containing a set of values that are passed to components on creation using a naming convention similar to HTML-tag attributes. They are data passed down from a parent component to a child component.The primary purpose of props in React is to provide following component functionality:1.Pass custom data to your component.
2.Trigger state changes.
3.Use via this.props.reactProp inside component’s render() method.
For example, let us create an element with reactProp property:[php][/php]
This reactProp (or whatever you came up with) name then becomes a property attached to React’s native props object which originally already exists on all components created using React library.
[php]props.reactProp[/php]
10.What is the difference between state and props?
Ans. Both props and state are plain JavaScript objects. While both of them hold information that influences the output of render, they are different in their functionality with respect to component. Props get passed to the component similar to function parameters whereas state is managed within the component similar to variables declared within a function.
11.Why should we not update the state directly?
Ans. If you try to update state directly then it won’t re-render the component.[php]//Wrong
this.state.message = ‘Hello world’;[/php]
Instead use setState() method. It schedules an update to a component’s state object. When state changes, the component responds by re-rendering.
[php]//Correct
this.setState({ message: ‘Hello World’ });[/php]
Note: You can directly assign to the state object either in constructor or using latest javascript’s class field declaration syntax.
12.What is the purpose of callback function as an argument of setState()?
Ans. The callback function is invoked when setState finished and the component gets rendered. Since setState() is asynchronous the callback function is used for any post action.Note: It is recommended to use lifecycle method rather than this callback function.[php] () => console.log(‘The name has updated and component re-rendered’));[/php]
13.What is the difference between HTML and React event handling?
Ans. Below are some of the main differences between HTML and React event handling,1.In HTML, the event name should be in lowercase:[php][/php]
Whereas in React it follows camelCase convention:
[php]
1.In HTML, you can return false to prevent default behavior:
[php]href=”#” onclick=’console.log(“The link was clicked.”); return false;’ />[/php]
Whereas in React you must call preventDefault() explicitly:
[php]function handleClick(event) {
event.preventDefault();
console.log(‘The link was clicked.’);
}[/php]
1.In HTML, you need to invoke the function by appending () Whereas in react you should not append () with the function name. (refer “activateLasers” function in the first point for example)
14.How to bind methods or event handlers in JSX callbacks?
Ans. There are 3 possible ways to achieve this:1.Binding in Constructor: In JavaScript classes, the methods are not bound by default. The same thing applies for React event handlers defined as class methods. Normally we bind them in constructor.[php]class Component extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
// …
}
}[/php]
1.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.
[php]handleClick = () => {
console.log(‘this is:’, this);
};[/php][php][/php]
1.Arrow functions in callbacks: You can use arrow functions directly in the callbacks.
[php][/php]
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.
15.How to pass a parameter to an event handler or callback?
Ans. You can use an arrow function to wrap around an event handler and pass parameters:[php] this.handleClick(id)} />[/php] This is an equivalent to calling .bind:
[php][/php]
Apart from these two approaches, you can also pass arguments to a function which is defined as arrow function
[php];
handleClick = (id) => () => {
console.log(‘Hello, your ticket number is’, id);
};[/php]
16.What are synthetic events in React?
Ans. SyntheticEvent is a cross-browser wrapper around the browser’s native event. It’s API is same as the browser’s native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers
17.What are inline conditional expressions?
Ans. You can use either if statements or ternary expressions which are available from JS to conditionally render expressions. Apart from these approaches, you can also embed any expressions in JSX by wrapping them in curly braces and then followed by JS logical operator &&.[php]
Hello!
;
{
messages.length > 0 && !isLogin ? (
You have {messages.length} unread messages.
) : (
You don’t have unread messages.
);
}[/php]
18.What is “key” prop and what is the benefit of using it in arrays of elements?
Ans. A key is a special string attribute you should include when creating arrays of elements. Key prop helps React identify which items have changed, are added, or are removed.Most often we use ID from our data as key:[php]const todoItems = todos.map((todo) =>
- {todo.text}
);[/php]
When you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort:
[php] const todoItems = todos.map((todo, index) =>
- {todo.text}
);[/php]
Note:
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.
19.What is the use of refs?
Ans. The ref is used to return a reference to the element. They should be avoided in most cases, however, they can be useful when you need a direct access to the DOM element or an instance of a component.
20.How to create refs?
Ans. There are two approaches1.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.[php]class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return
;
}
} [/php]
1.You can also use ref callbacks approach regardless of React version. For example, the search bar component’s input element accessed as follows,
[php]class SearchBar extends Component {
constructor(props) {
super(props);
this.txtSearch = null;
this.state = { term: ” };
this.setInputSearchRef = (e) => {
this.txtSearch = e;
};
}
onInputChange(event) {
this.setState({ term: this.txtSearch.value });
}
render() {
return (
{this.state.term});
}
}[/php]
You can also use refs in function components using closures. Note: You can also use inline ref callbacks even though it is not a recommended approach