JavaScript Render Normal JavaScript in React

So I'm new to react and I'm trying to understand how non-react code and react code can interact.

So for example, let's say I have a library which draws a circle in a DOM element with syntax like this: c = new Circle('#container')

and as soon as that code is executed, a circle is drawn in the DOM element with an id of container.

If I wanted to create a React component based off of this, how would I go about it? This is what I had in mind:

var circ = React.createClass({

    componentDidMount: function(){
      c = new Circle('#container')
    },

    render: function(){
      return (
        <div id="container"></div>
    );
   }

});

Is this acceptable, or is there a better way to go about this?

Answer:1

Yes, your approach is right, but there are few moments you need to keep in mind.

  1. React can rerender it's DOM and bindings of Circle library can be broken. So use shouldComponentUpdate() to control render process. https://facebook.github.io/react/docs/react-component.html#shouldcomponentupdate
  2. Don't forget to unbind Circle library on component destroying. https://facebook.github.io/react/docs/react-component.html#componentwillunmount
Answer:2

For example if you want to interact with DOM, you can add special ref prop to some element like:

<div ref="blabla"></div>

and interact using regular javascript api, or other non-react apis.

ReactDOM.findDOMNode(this.refs["blabla"]).style.display='none'

Here I'm hiding element with ref "blabla"

Answer:3

So I more than one dynamicly generated elements with the same class name that I am trying to check input for in jQuery. Instead of it letting me click on both, it is just letting me click the first ...

So I more than one dynamicly generated elements with the same class name that I am trying to check input for in jQuery. Instead of it letting me click on both, it is just letting me click the first ...

  1. class click not working jquery
  2. class selector not working jquery
  3. remove class not working jquery
  4. jquery addclass not working
  5. jquery toggleclass not working
  6. has class not working jquery

I wrote a jQuery function to add and remove some input, but I got this error, pls help me ! I using jQuery 3.2.1 and Laravel 5.4 Uncaught TypeError: Cannot read property 'remove' of undefined Here ...

I wrote a jQuery function to add and remove some input, but I got this error, pls help me ! I using jQuery 3.2.1 and Laravel 5.4 Uncaught TypeError: Cannot read property 'remove' of undefined Here ...

I have created custom tooltip for my chart in chartjs. But the issue comes when I hover on the chart, the tooltip starts flickering. Anyone knows how can I prevent this? Thanks! if (!tooltip) { ...

I have created custom tooltip for my chart in chartjs. But the issue comes when I hover on the chart, the tooltip starts flickering. Anyone knows how can I prevent this? Thanks! if (!tooltip) { ...

Trying to authenticate users on my ionic application through an external service and I need to use cordovas In app browser! The code works perfectly on android however on iOS the "loadstop" event ...

Trying to authenticate users on my ionic application through an external service and I need to use cordovas In app browser! The code works perfectly on android however on iOS the "loadstop" event ...

  1. cordova browser event