0

I just want to know if there is any way I can "draw" in my index.html multiple <div id = "x" /> <div id = "y" /> with REACT, i mean.. i have all my site on index.html with all my template, so i only need to use REACT on an specifics sections...

enter image description here

i tried this i didnt work

HTML

<div id="test" />
<div id="app" />
<script src="public/bundle.js" charset="utf-8"></script>

JSX

import React from 'react';
import {render} from 'react-dom';

class App extends React.Component {
  render () {
    return (<h1>App</h1>);
  }
}
class Test extends React.Component {
  render () {
    return (<h1>Test</h1>);
  }
}

render(<App/>, document.getElementById('app'));
render(<Test/>, document.getElementById('test'));

then when i load the page only prints the <h1>Test</h1>... why?

12
  • 1
    Try closing your div tags. Div is not self-closing. Commented Sep 15, 2016 at 18:52
  • 1
    Are you using the dev version of React? If so, you should be getting some helpful errors in the console. Commented Sep 15, 2016 at 19:35
  • 3
    You cannot do return ( <div>1</div> <div>2</div> ); place them within a parent div, like return ( <div> <div>1</div> <div>2</div> </div> );, What do you see in the developer console? Commented Sep 15, 2016 at 19:45
  • 1
    You'd get an error message in the console. @Random User is totally right. Commented Sep 15, 2016 at 19:53
  • 1
    wrap your App component in a div as <div><div>1</div><div>2</div></div> Commented Sep 16, 2016 at 6:58

3 Answers 3

1

I created a JSFiddle to try a few things out here: http://jsfiddle.net/pof580fd/1/

I found out that by explicitly closing each of the <div> tags I could get it to work, i.e.:

<div id="test"></div>
<div id="app"></div>

I did a little research and it appears that as div is not one of the HTML5 "void elements" (Listed here: https://www.w3.org/TR/html5/syntax.html#void-elements) it is not self-closing and you must use </div>. See this SO question for more details: Are (non-void) self-closing tags valid in HTML5?

Possibly some browsers are lenient about this (I'm using Chrome 52 right now) - but React is not, it appears. The error message I see in the console is:

Uncaught Error: Invariant Violation: _registerComponent(...): Target container is not a DOM element.

(Make sure you're using the "dev" version of React to see these)

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

1 Comment

Thanks you! so its the self-closing in the html.
1

You can create a new component and call from that.

import App from "./App";
import Test from "./Test";
class Program extends React.Component {
  render() {
    <div>
      <div id="app"><App /></div>
      <div id="test"><Test /></div>
    </div>
  }
}

and then call

render(<Program />), document.getElementById('...'));

1 Comment

is not what I need, I know it can be done that way but still do render a single div in my html...check my edit.
0

We created a framework to do this with great success.

Have a look at React Habitat.

With it you can just register components eg:

container.register('SomeReactComponent', SomeReactComponent);

Then they auto resolve in the dom via:

<div data-component="SomeReactComponent"></div>

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.