×

React Component API

React Component API

It is a top-level API. It provides reusability to the code in the application and makes it completely individual. It has several methods for:

  • Creating Elements.
  • Transforming Elements.
  • Fragments.

Now, we are explaining the three important methods that are available in the React component API:

  • findDOMNode()
  • setState()
  • forceUpdate()

findDOMNode()

For manipulating the DOM, you have to use ReactDOM.findDOMNode() method. This method helps us to find or access the underlying DOM node.

Syntax:

ReactDOM.findDOMNode(component);

Example:

App.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom'; 
class App extends React.Component {
constructor() {
super()
this.findDomNodeHandler1 = this.findDomNodeHandler1.bind(this);
this.findDomNodeHandler2 = this.findDomNodeHandler2.bind(this);
};
findDomNodeHandler1() {
var div1 = document.getElementById('One');
 ReactDOM.findDOMNode(div1).style.color = 'red';
}
findDomNodeHandler2() {
var div2 = document.getElementById('Two');
ReactDOM.findDOMNode(div2).style.color = 'blue';
}
render() {
:return (

ReactJS Find DOM Node Example

Red Color

Blue Color

) } } export default App;

Output:

Reactjs find Dom node Example

When you click on the button, the color of the text gets changed. The output after clicking on the button is as follows:

React js Find dom node

setState()

This method is used for updating the state of the component. Instead of replacing the state immediately, it only adds changes to the original state. It is a primary method that is used for updating the user interface (UI) as a response to the event handlers and the responses of the server.

Syntax:

this.stateState(object newState[, function callback]);

Example:

App.js

import React, { Component } from 'react';
import PropTypes from 'prop-types'; 
class App extends React.Component { 
constructor() {
super();
this.state = {
data: "!!!! Hello World !!!!"
}
this.updateSetState = this.updateSetState.bind(this);
}
updateSetState() {
this.setState({
data:"Welcome to tutorialandexample.com"
});
}
render() {
return (

{this.state.data}

) } } export default App

Main.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js'; 
ReactDOM.render(, document.getElementById('app')); 

Output:

Hello world!

After clicking on the SET STATE button, you will get the following output:

welcome to tutorialandexample

forceUpdate()

This method allows the manually updating of the components.

Syntax:

Component.forceUpdate(callback);

Example:

App.js

import React, { Component } from 'react';
class App extends React.Component {
constructor() {
super()
this.forceUpdateState = this.forceUpdateState.bind(this);
}
forceUpdateState() {
this.forceUpdate();
};
render() {
return (

Example of Force Update method

Example to Generate Random Number

Random number: {Math.random()}

); } } export default App;

Output:

Example of force update method

When you click on the ForceUpdate button, you will always get the new random number. It is shown in the following image:

to generate random number

Related Topics

React Component API

React Component API It is a top-level API. It provides reusability to the code in the application and makes it completely individual. It has several methods for: Creating Elements.Transforming Elements.Fragments. Now, we are explaining the three...

2 minutes read.

ReactJS Tutorial

ReactJS Introduction React Tutorial helps you to understand the basic and some advanced concepts of ReactJS. As of now, it is the essential front-end library that is developed by a...

3 minutes read.

React Lists

React Lists Lists are used for displaying the data in an ordered format and mainly used to display the menus on websites. The creating of lists in React is as similar...

2 minutes read.

React Props Validation

React Props Validation As we know, props are a suitable mechanism for passing the read-only attributes to react components. The props are required to be correctly used in the component. If it is...

5 minutes read.

ReactJS vs AngularJS

Difference between ReactJS and AngularJS AngularJS AngularJS is the JavaScript framework that is open source, and it is also used to build a dynamic web application. It is developed in 2009 by...

5 minutes read.

React Forms

React Forms Forms are the important part of any web application. It allows the interaction of the user with the app as well as the gathering of the information from the...

3 minutes read.

React Conditional Rendering

React Conditional Rendering In React, the working of the conditional rendering is similar to the condition works in JavaScript. We use JavaScript operators for creating elements that represent the current state, and then the React...

3 minutes read.

React Map

React Map The map() is the standard function of JavaScript, which can be called at any array. The map() method is used for traversing and displaying a list of the similar objects of...

2 minutes read.

React Table

React Table The table represents an arrangement that organizes the information in the form of rows and columns. The table is mainly used for storing and displaying the data within a structured format. Features of...

2 minutes read.

React Error Boundaries

React Error Boundaries In the past, the errors of JavaScript within the components were used to corrupt the internal state of react and cause it to emit the cryptic errors on the next...

3 minutes read.

Pros and Cons of ReactJS

Pros and Cons of ReactJS There are various advantages, and disadvantages of ReactJS are as follows: Benefits of ReactJS 1. Easy to Learn and Use: ReactJS is very easy to use and learn. It has a good...

3 minutes read.

Comparison between React and Vue

Comparison between React and Vue React, and Vue both are the two most famous libraries of JavaScript that are used for creating thousands of websites today. The React and Vue both...

3 minutes read.

React JSX

React JSX As we know, all of the React components include a render function. The Render function specifies the HTML output of a React component. JSX is an acronym of JavaScript extension that...

3 minutes read.

React Keys

React Keys A key can be defined as a unique identifier. In React, it is used for identifying the items that have changed, deleted, and updated from the lists. React Keys are helpful...

3 minutes read.

React CSS

What is React CSS React CSS is used to provide the style to the React applications. The style attribute adds dynamically-computed styles at render time, and it is one of the most used...

5 minutes read.

React Router

What is React Router? Routing is a process by which a user can direct to different pages based on their actions and requests. ReactJS router is generally used to develop single...

10 minutes read.

React Constructors

What is constructor? The constructor is a method that is used for initializing the state of the object in the class. It calls automatically during the creation of an object in...

4 minutes read.

React Animation

React Animation The animation is a procedure in which an image is manipulated to appear as a moving image. It is widely used to create an interactive web application. In React, we have to...

4 minutes read.

React Component Life-Cycle

React Component Life-Cycle In ReactJS, the creation process of every component includes several lifecycle methods. These methods, together stated as component's lifecycle. Four phases of the component's life cycle are as...

4 minutes read.

React Props

React Props A prop is an abbreviation of ‘properties.' State and props are mainly different from each other because props are immutable. Props are the read-only components. It is the object that stores the attribute...

3 minutes read.