1
var MyBox = React.createClass({
    rawMarkup: function() {
        var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
        return { __html: rawMarkup };
    },
    getElements: function () {
        //I want to be able to return raw HTML string
        //from here to inject into another element via jQuery
    },
    loadCommentsFromServer: function() {
    },
    getInitialState: function() {
        return {data: []};
    },
    componentDidMount: function() {
        this.loadCommentsFromServer();
        window.drill = this;
    },
    render: function() {
        return (
            <ul>
                <li>item1</li>
                <li>item2</li>
                <li>item3</li>
            </ul>
        );
    }
});

via Javascript using jQuery I want to inject the output to another element like below

  $(clicked).html(window.drill.getElements());
4
  • 1
    Your window.this is the react component, not the actual HTML. Give your component an id, then you can simply read it from the DOM using jQuery. Commented Oct 21, 2015 at 8:29
  • Yup I am reading it via window.drill.getElements(). I hope you can give me a code sample. I am not familiar with component having IDs Commented Oct 21, 2015 at 8:36
  • Oh I get it. <ul> element should have id then read in from the DOM and I will take care of the data via this.state.data right? Commented Oct 21, 2015 at 8:39
  • My problem will be the rendering of the elements new values base on what the user's clicked. Those clickable items are not generated by ReactJs. Just a Bootstrap accordion/Panel rendered via AngularJs Commented Oct 21, 2015 at 8:42

1 Answer 1

2

Try to avoid having two owners/ updaters of the same component. You should really choose between:

A. make your react code the owner of all HTML inside the component.
B. do not use react to render the code, but let Angular/ BootstrapJS or whatever manipulate the DOM.

If you go with A, then change your react code to:

componentDidMount: function() {
    this.loadCommentsFromServer();
    // You do not need the window.drill = this here
},
render: function() {
    return (
        <ul id='foo'>
            <li>item1</li>
            <li>item2</li>
            <li>item3</li>
        </ul>
    );
}

And then have your code read the contents of the DOM by through javascript getElementByID('foo') of jQuery $('#foo'), which you can copy to somewhere else.

If you want to update the contents of the component based on what user clicks, your react component needs a listener to changes somewhere else (in your model, not in the DOM), and re-render the component with new props.
Then when the user clicks something, the onClick code should update model, which triggers re-render.

Hope this makes sense.

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

2 Comments

Yup make sense. This is a great help. I could still use window.drill = this to trigger the listener and let React handle the output. And jquery for DOM manipulation just like you suggested. Thanks.
The reason for such is because the data source location will come from Angular output which I can pass to React Listener to fetch the said data.Thanks.

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.