BrainLikeADullPencil BrainLikeADullPencil - 3 months ago 153
React JSX Question

How to append to dom in React?

Here's a js fiddle showing the question in action.

In the render function of a component, I render a div with a class

.blah
. In the
componentDidMount
function of the same component, I was expecting to be able to select the class
.blah
and append to it like this (since the component had mounted)

$('.blah').append("<h2>Appended to Blah</h2>");


However, the appended content does not show up. I also tried (shown also in the fiddle) to append in the same way but from a parent component into a subcomponent, with the same result, and also from the subcomponent into the space of the parent component with the same result. My logic for attempting the latter was that one could be more sure that the dom element had been rendered.

At the same time, I was able (in the
componentDidMount
function) to
getDOMNode
and append to that

var domnode = this.getDOMNode();
$(domnode).append("<h2>Yeah!</h2>")


yet reasons to do with CSS styling I wished to be able to append to a div with a class that I know. Also, since according to the docs
getDOMNode
is deprecated, and it's not possible to use the replacement to
getDOMNode
to do the same thing

var reactfindDomNode = React.findDOMNode();
$(reactfindDomNode).append("<h2>doesn't work :(</h2>");


I don't think
getDOMNode
or
findDOMNode
is the correct way to do what I'm trying to do.

Question: Is it possible to append to a specific id or class in React? What approach should I use to accomplish what I'm trying to do (
getDOMNode
even though it's deprecated?)

var Hello = React.createClass({
componentDidMount: function(){

$('.blah').append("<h2>Appended to Blah</h2>");
$('.pokey').append("<h2>Can I append into sub component?</h2>");
var domnode = this.getDOMNode();
$(domnode).append("<h2>appended to domnode but it's actually deprecated so what do I use instead?</h2>")

var reactfindDomNode = React.findDOMNode();
$(reactfindDomNode).append("<h2>can't append to reactfindDomNode</h2>");

},
render: function() {
return (
<div class='blah'>Hi, why isn't the h2 being appended here?
<SubComponent/>
</div>
)
}
});

var SubComponent = React.createClass({

componentDidMount: function(){
$('.blah').append("<h2>append to div in parent?</h2>");
},

render: function(){
return(
<div class='pokey'> Hi from Pokey, the h2 from Parent component is not appended here either?
</div>
)
}
})

React.render(<Hello name="World" />, document.getElementById('container'));

Answer

In JSX, you have to use className, not class. The console should show a warning about this.

Fixed example: https://jsfiddle.net/69z2wepo/9974/

You are using React.findDOMNode incorrectly. You have to pass a React component to it, e.g.

var node = React.findDOMNode(this);

would return the DOM node of the component itself.

However, as already mentioned, you really should avoid mutating the DOM outside React. The whole point is to describe the UI once based on the state and the props of the component. Then change the state or props to rerender the component.