Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJs: Is it possible to render DOM to document by class name?

I'm learning ReactJs and really like it. I wanted to ask if we can render virtual DOM to document by class name.

React.render(
    <CommentBox url="data/comments.json" pollInterval={2000} />,
    document.getElementById('class')
);

I tried to pass

getElementByClassName('class')

as second argument to react's render method and it doesn't work. Does React only render DOM to nodes with ID's only or is there's a workaround to use nodes with classes too?

like image 387
Waleed Ahmad Avatar asked Sep 14 '15 02:09

Waleed Ahmad


1 Answers

It seems you have used a wrong method.

Probably you should use getElementsByClassName instead of getElementByClassName. And DO NOT forget getElementsByClassName returns an array-like obj HTMLCollection, so picking the first element is necessary.

React.render(
  <CommentBox url="data/comments.json" pollInterval={2000} />,
  document.getElementsByClassName('className')[0]
);

For more infomation, check out the docs.

like image 120
xcatliu Avatar answered Oct 20 '22 22:10

xcatliu