0

I am using react.js as the templating library in my node.js application . I am using jsx syntax for my react components. I have a route like /entities which renders the entities.jsx file (it is a view) . I have the following code in my entities.jsx file. I am trying to call the onclickhandler in my code and its not working.I am not rendering it explicitly using React.render but the component is showing in the browser when I hit localhost:/entities but the onclick handler isnt firing.

  var MyComponent = React.createClass({
    alertMessage: function() {
       alert(this.props.name);                             
   },
    render: function () {
      return (
       <div onClick={this.alertMessage}>
        Hello {this.props.name}
       </div>
     );
    }
 });

The examples that I have seen online render a jsx script embedded inside an html and a component rendered inside an html div tag but I am trying to use react.js in my node.js application. I dont want to render it inside an html tag but the component should render based on a url and should be dynamic

I have the following code snippet in my index.js file in my node application . I hit the view 'entities' through my index.js file that renders the view entities.

app.get('/entities', function(req, res) {
  res.render('entities', {
    title: 'Entities - Turkey Disclosure',
    name: 'Entities',
    selection: 'header-entities'
  });
});
5
  • That doesn't work. Apparently that is true for ES6 syntax only Commented Apr 18, 2016 at 10:43
  • your example works as you expect fiddle Commented Apr 18, 2016 at 10:44
  • I am not using React.render. When I do, I get an error saying ReferenceError:document is not defined Commented Apr 18, 2016 at 10:45
  • How do you render your component? could you add code sample? Commented Apr 18, 2016 at 10:48
  • @The ..Please see the edit Commented Apr 18, 2016 at 10:53

1 Answer 1

1

If I get you right you're using React.renderToString() to render react in node application. React doesn't insert onClick and other event statements directly to markup. If you want to keep those events you should call React.render() on a node that already has server-rendered markup. React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.

There's a nice article explaining difference between three rendering methods in React.

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

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.