By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to convert this class component to functional component
JavaScript

Javascript – How to convert this class component to functional component

Admin
Last updated: 2024/02/10 at 7:08 AM
Admin
Share
3 Min Read
How to convert this class component to functional component

Problem:

I have this class component and i want to convert it in a functional component, how can I do? I tried to do it but i have some issue whit the this.chart properties in the componentDidMount() method, where i have to put it in functional component? I also tried to use the hook useRef() in the place of createRef() is it correct? Anyone can provide me a full converted example so i can understand how to do it? Thank you

Contents
Problem:Solution:
import React, { Component } from 'react';
import OrgChart from '@balkangraph/orgchart.js';

export default class Chart extends Component {
  constructor(props) {
    super(props);
    this.divRef = React.createRef();
  }

  shouldComponentUpdate() {
    return false;
  }

  componentDidMount() {
    this.chart = new OrgChart(this.divRef.current, {
      nodes: this.props.nodes,

      nodeBinding: {
        field_0: 'name',
        img_0: 'img',
      },
    });

    this.chart.on('init', function () {
      console.log('init');
    });

    this.chart.on('click', function () {
      console.log('click');
    });
  }

  render() {
    return <div id="tree" ref={this.divRef}></div>;
  }
}

Solution:

It’s not that much different really.

The main thing is the use of the React.useEffect..

Also note, in your above your not doing anything with chart on component unmount, if you return a function from useEffect you can use that to cleanup on unmount. Also note the need for this is now gone..

Below is your code converted. E&OE

import React, { useEffect } from 'react';
import OrgChart from '@balkangraph/orgchart.js';

export default function Chart(props) {
  const divRef = React.createRef();
  useEffect(() => {
    const chart = new OrgChart(divRef.current, {
      nodes: props.nodes,
      nodeBinding: {
        field_0: 'name',
        img_0: 'img',
      },
    });

    chart.on('init', function () {
      console.log('init');
    });

    chart.on('click', function () {
      console.log('click');
    });

  }, []);
  return <div id="tree" ref={divRef}></div>
}

As pointed out above your current code is not doing anything about unmounting the component, often things like chart components have a destroy method etc, looking at the docs for OrgCharts if it’s the same -> https://balkan.app/OrgChartJS/API/classes/OrgChart#destroy

So to really finish the component off,. I would put this in the useEffect hook

useEffect(() => {
  const chart = ....
  ....
  return () => {
    chart.destroy();
  }
},[]);

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article how to get prefix values in angular input field Javascript – how to get prefix values in angular input field
Next Article Reduce function shows NaN or Undefined when used along with map() function in JSX Javascript – Reduce function shows NaN or Undefined when used along with map() function in JSX
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright Ā© 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?