Alexandre Demelas
Alexandre Demelas

Reputation: 4690

ReactJs + Babel - Defining components using separate files

Trying to create a component class with separate files

Index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="../../assets/libs/react-0.13.3/build/react.js"></script>
    <script src="../../assets/libs/babel.min.js"></script>
  </head>
  <body>
    <section class="reactive"></section>
    <script type="text/babel" src="../../components/AppBar/NavBar.js"></script>
    <script type="text/babel">
      var
        reactiveNode = document.querySelector('.reactive');
      React.render( <NavBar value='hello world'/>, reactiveNode );
    </script>
  </body>
</html>

Navbar.js

var NavBar = React.createClass({
  render: function ()
  {
    return ( <h1>{this.props.value}</h1> );
  }
});

After run, i'm having the log:

XHR finished loading: GET "http://localhost:3000/app/components/AppBar/NavBar.js"

Uncaught ReferenceError: NavBar is not defined

Only works if create the class in the index.html file

Upvotes: 3

Views: 1664

Answers (2)

Yasitha Bandara
Yasitha Bandara

Reputation: 2375

If you are using OOP react,it should be like this

    class NavBar extends React.Component{
       // ...
    });
    window.NavBar = NavBar;

Upvotes: 0

Alexandre Demelas
Alexandre Demelas

Reputation: 4690

The problem was with the scope of the variable.

Solve by use

window.NavBar = React.createClass({
   // ...
});

Upvotes: 3

Related Questions