Home >>Interview Questions >React Interview Questions and Answers
Below are the following advantages of react:
These are the main features of react:
These are the following limitations of react:
JSX is a shorthand for JavaScript XML. This is a type of file used by React that uses JavaScript's expressiveness along with HTML as the syntax of templates. This makes understanding of the HTML file really easy. This file makes robust applications, and boosts their performance.
render(){
return(
<div>
<h1> Hello World from Edureka!!</h1>
</div>
);
}
Only JavaScript objects can be read by browsers but JSX is not in a regular JavaScript object. To enable a browser to read JSX, first, JSX file needs to be transformed into a JavaScript object using JSX transformers such as Babel, and then passed to the browser.
Props in React stand for "Properties." They are component inputs which are read-only. Props is an entity that stores the value of a tag's attributes and appears to work similar to HTML attributes. This provides a way to pass data across the program from the parent to the child components.
It is equivalent to function arguments and passes to the element in the same way as the arguments in a function passed through.
Props are permanent so we are unable to change the props from inside the component. We may add attributes, called props, within the components. These attributes are available as this.props in the component, and can be used in our rendering method to render dynamic data.
Components in React are the building blocks for react applications. These components divide the entire UI of the React application into small pieces of code which are independent and reusable. React renders each of these components separate, without affecting the rest of the UI. So, in React, we can say that everything is a part.
A Virtual DOM is a lightweight JavaScript object, representing real DOM in memory. It is an intermediary step between the call to the render function and the display of elements on the screen. It is similar to a node tree that lists the elements as objects and their properties, their attributes and content. The rendering function creates a React component node tree and then updates this node tree in response to the data model mutations caused by different user or system actions.
Angular | React | |
---|---|---|
Author | Facebook Community | |
Developer | Misko Hevery | Jordan Walke |
Initial Release | October 2010 | March 2013 |
Language | JavaScript, HTML | JSX |
Type | Open Source MVC Framework | Open Source JS Framework |
Rendering | Client-Side | Server-Side |
Data-Binding | Bi-directional | Uni-directional |
DOM | Regular DOM | Virtual DOM |
Testing | Unit and Integration Testing | Unit Testing |
App Architecture | MVC | Flux |
Performance | Slow | Fast, due to virtual DOM. |
For through React component a render() function is mandatory. The function Render is used to return the HTML that you want to view in a variable. If more than one HTML element needs to be rendered, you need to group together within the single enclosing tag (parent tag) such as <div>, <form>, <group> etc. Every time it is invoked this function returns the same result.
Example:
import React from 'react'
class App extends React.Component {
render (){
return (
<h1>Hello World</h1>
)
}
}
export default App
The State is an updatable structure that holds the component data and information. In response to user behavior or system event, this can be changed over the lifetime of the component. It is the core of the component react which determines the component 's behavior and how it will render. You have to keep it as easy as possible.
Let’s see an Example-
import React from 'react'
class User extends React.Component {
constructor(props) {
super(props)
this.state = {
message: 'Welcome to JavaTpoint'
}
}
render() {
return (
<div>
<h1>{this.state.message}</h1>
</div>
)
}
}
export default User
This.setState() method helps us to update the state of a component. That method does not always immediately replace the State. Instead, it just adds changes to the original State. It is a primary method which is used in response to event handlers and server responses to update the user interface(UI).
Example:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class App extends React.Component {
constructor() {
super();
this.state = {
msg: "Welcome to JavaTpoint"
};
this.updateSetState = this.updateSetState.bind(this);
}
updateSetState() {
this.setState({
msg:"Its a best ReactJS tutorial"
});
}
render() {
return (
<div>
<h1>{this.state.msg}</h1>
<button onClick = {this.updateSetState}>SET STATE</button>
</div>
);
}
}
export default App;
The function Arrow is the latest feature of the standard ES6. If you need to use arrow functions, binding no event to 'this' is not necessary. Here, the scope of 'this' is global and not limited to any calling function. So if you use Arrow Function, you don't need to add 'this' within the constructor. It is also called the functions' fat arrow. '(= >).
//General way
render() {
return(
<MyInput onChange={this.handleChange.bind(this) } />
);
}
//With Arrow Function
render() {
return(
<MyInput onChange={ (e) => this.handleOnChange(e) } />
);
}
An event is an action that occurs such as a mouse click, loading a web page, pressing a key, window resizes etc. as a result of the user action or system generated event. The event handling system in React is very similar to handling events in elements in DOMs. The event handling system for React is known as Synthetic Event, which is a cross-browser wrapper of the native event of the browser.
There are some syntactic differences in handling React events, which are:
A synthetic event is an object that serves as a wrapper of cross-browsers around the native event of the browser. It incorporates the behavior of the native events of various browsers into one API, including stopPropagation() and preventDefault().
Let’s take an Example-
function ActionLink() {
function handleClick(e) {
e.preventDefault();
console.log('You had clicked a Link.');
}
return (
<a href="#" onClick={handleClick}>
Click_Me
</a>
);
}
Forms allow users to interact with the application as well as collect user information. Forms can perform a lot of tasks like user authentication, user adding, searching, filtering, etc. A type can contain text fields, buttons, checkbox, button Radio, etc.
React provides a stately, reactive approach to form-building. The React forms are similar to those in HTML files. But in React, the component's state property is only updated via setState() and their submission is handled by a JavaScript function. This function has full access to the data that is entered into a form by the user.
import React, { Component } from 'react';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {value
Lists are used in ordered format to view data. In React, it is possible to build lists in the same way as we do in JavaScript. Using the map() function we can traverse the elements of the list.
Let’s take an Example-
import React from 'react';
import ReactDOM from 'react-dom';
function NameList(props) {
const myLists = props.myLists;
const listItems = myLists.map((myList) =>
<li>{myList}</li>
);
return (
<div>
<h2>Rendering Lists inside component</h2>
<ul>{listItems}</ul>
</div>
);
}
const myLists = ['Peter', 'Sachin', 'Kevin', 'Dhoni', 'Alisa'];
ReactDOM.render(
<NameList myLists={myLists} />,
document.getElementById('app')
);
export default App;
The following way, you can add two or more components:
import React from 'react'
class App extends React.Component {
render (){
return (
<h1>Hello World</h1>
)
}
}
class Example extends React.Component {
render (){
return (
<h1>Hello JavaTpoint</h1>
)
}
}
export default App
A key is a single identifier. This is used in React to identify what items from the Lists have changed, modified, or deleted. It's useful when we build components dynamically, or when users adjust the lists. It also helps to determine which components need to be re-rendered in a collection, rather than re-rendering the entire set of components each time. It improves efficiency in applications.