3

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?

3 Answers 3

3

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"

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, but how would you recommend I do that with the circle object as mentioned in the question? I can't instantiate circle without giving it a DOM element to attach it to, and I don't want to confuse react by creating an element without letting react know.
this ref api is deprecated. ref={el => this.bar = el}. you won’t need to traverse the DOM—it’s the actual HTMLElement or React.Component so you can work with DOM Element methods or instance methods. componentDidMount can then do new Circle(this.bar)
2

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

Comments

1

If I understand it right, the Circle() will print out a element inside the DOM element you provide as a parameter. If it was me I would have a diferent approach on that. Instead of using that component to manage/edit the DOM element, use the result result of it to be printed inside the DOM element. So, if the Circle() returns a SVG code grab it and print it out inside the #container. If it is a script to generate a base64 image, get the result to print inside #.

I would avoid to use react as a DOM manager, as you can do with jQuery, instead try to think react as a blocks/components manager, so you replace a component with another.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.