1

This is the second tutorial that I am following due to failure in versions and dependencies in the earlier. As you can guess I am just trying to get started with react here.

I created a controller called Appointments. This is my controller jsx inside the components folder.

app/assets/js/components/appointments.jsx

var Appointments = React.Component({
    render: function() {
        return (
         <h1>React calender</h1>
        )
    }
});

appointments/index.html.erb

<%= react_component 'Appointments' %>

And I restarted rails server. All I see is a blank browser and console showing this

Uncaught ReferenceError: Appointments is not defined 
5
  • have you tried class Appointments extends React.Component Commented Dec 4, 2017 at 8:51
  • @brandNew No I havent. Where should I add this ? Commented Dec 4, 2017 at 8:52
  • Instead of var Appointments = React.Component, try class Appointments extends React.Component Commented Dec 4, 2017 at 8:53
  • I just replaced that line like you said. Now I am getting a syntax error at }); in appointments.jsx. Saying unexpected token Commented Dec 4, 2017 at 8:57
  • I have posted an answer, have a look at it. Commented Dec 4, 2017 at 9:00

1 Answer 1

2

What you most probably meant to do is this

const Appointments = React.createClass({
    // ...
    render() {
    return <div>{this.state.hello}</div>;
  }
});

Which is not a very good thing if you plan to or have internal state and/or refs

It is recommended to rather do it like this

class Appointments extends React.Component {
   // ...
  render() {
    return <div>{this.state.hello}</div>;
  }
}

So in your case your component should change from:

var Appointments = React.Component({
  render: function() {
    return (
     <h1>React calender</h1>
    )
  }
});

to this:

class Appointments extends React.Component {
  render() {
    return (
     <h1>React calender</h1>
    )
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is now displaying the h1 element. But now I am getting a console error like Uncaught ReferenceError: require is not defined. I been getting this error in another tutorial that I was following. It would be really great if you tell me how to get rid of this. I have actually posted a question about that issue but wasnt able resolve anything.
May you please share the link to the question?

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.