Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

React Suite DOMHelper Component

  • Last Updated : 30 Jun, 2021

React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. DOMHelper component allows the user to directly manipulate the DOM for some considerations. We can use the following approach in ReactJS to use the React Suite DOMHelper Component.

DOMMouseMoveTracker: It is used for the mouse drag tracker. This method takes the following parameters:

  • onMove: It is a function that is triggered when the movement is active.
  • onMoveEnd: It is a function that is triggered when the movement is stopped.
  • node: It is used to denote the HTML element.

Creating React Application And Installing Module:

  • Step 1: Create a React application using the following command:

    npx create-react-app foldername

     

  • Step 2: After creating your project folder i.e. foldername, move to it using the following command:

    cd foldername
  • Step 3: After creating the ReactJS application, Install the required module using the following command:

    npm install @rsuite/charts

Project Structure: It will look like the following.

Project Structure

Example 1: In the following example, we have used the addTheStyling method to add the style to our element at the click of the button.

Filename: App.js

App.js




import React from 'react'
import 'rsuite/dist/styles/rsuite-default.css';
import { Button, DOMHelper } from 'rsuite';
const { addStyle } = DOMHelper;
  
export default class App extends React.Component {
  
  // State holding initial style
  constructor(props) {
    super(props);
    this.state = {
      htmlCode: '<div class="view"></div>'
    };
  }
  
  // Function to set the states with latest style
  showView() {
    const htmlCode = this.container.innerHTML;
    this.setState({ htmlCode });
  }
  
  // Function to add the styles
  addTheStyling() {
    addStyle(this.view, { 'margin-top': '16px', 'color': 'orange' });
    this.showView();
  }
  
  render() {
    const { htmlCode } = this.state;
    return (
      <div>
        <h4>React Suite DOMHelper Component</h4>
        <Button appearance="primary" onClick={() => this.addTheStyling()}
        > Click to Add Style
        </Button>
  
        <div>{htmlCode}</div>
        <div ref={ref => { this.container = ref }}>
          <div className="view" ref={ref => { this.view = ref }} />
        </div>
      </div>
    );
  }
}

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:



Example 2: In the following example, we have used the hasClass method to check whether the element has a custom-named class or not, this function is triggered when the user clicks on the button.

Filename: App.js

suite




import React from 'react'
import 'rsuite/dist/styles/rsuite-default.css';
import { Button, DOMHelper } from 'rsuite';
const { hasClass } = DOMHelper;
  
export default class App extends React.Component {
  
    // State holding initial style
    constructor(props) {
        super(props);
        this.state = {
            htmlCode: `<div class="view" style="font-size: 16px; 
            color: orange;"></div>`
        };
    }
  
    render() {
        const { htmlCode } = this.state;
        return (
            <div>
                <h4>React Suite DOMHelper Component</h4>
                <Button appearance="primary" onClick={() =>
                    alert(hasClass(this.view, 'custom'))}
                > Click me to check hasClass method
                </Button>
  
                <div>{htmlCode}</div>
                <div ref={ref => { this.container = ref }}>
                    <div className="view" ref={ref => { this.view = ref }} />
                </div>
            </div >
        );
    }
}

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Reference: https://rsuitejs.com/components/dom-helper/


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!