Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js: Example in tutorial not working

I'm doing the React.js tutorial from http://facebook.github.io/react/docs/tutorial.html. Here are my files:

template.html:

<html>     <head>         <title>Hello React</title>         <script src="http://fb.me/react-0.8.0.js"></script>         <script src="http://fb.me/JSXTransformer-0.8.0.js"></script>         <script src="http://code.jquery.com/jquery-1.10.0.min.js"></script>     </head>     <body>         <div id="content"></div>         <script type="text/jsx" src='tut.js'>         </script>     </body> </html> 

and tut.js:

/** @jsx React.DOM */  var data = [     {author: 'Tldr', text: 'This is a comment'} ]  var CommentBox = React.createClass({     render: function() {         return (             <div className='commentBox'>                 <h1>Comments</h1>                 <CommentList data={this.props.data} />                 <CommentForm />             </div>         )     } })  var CommentList = React.createClass({     render: function() {         var commentNodes = this.props.data.map(function(comment) {                     return <Comment author={comment.author}>{comment.text}</Comment>             })         return (             <div className='commentList'>                 {commentNodes}             </div>         )     } })  var CommentForm = React.createClass({     render: function() {         return (             <div className='commentForm'>                 Hello World, I am a comment             </div>         )     } })  var Comment = React.createClass({     render: function() {         return (             <div className='comment'>                 <h2 className='commentAuthor'>                     {this.props.author}                 </h2>                 {this.props.children}             </div>         )     } })  React.renderComponent(     <CommentBox data={data} />,     document.getElementById('content') ) 

But when I open it in the browser, I just see a blank page without any comments. What am I doing wrong?

like image 307
tldr Avatar asked Jan 03 '14 12:01

tldr


People also ask

Is learning ReactJS difficult?

Both HTML and CSS are integral to any web development project. If you have these skills already, then learning React should be a relatively straightforward process. It has its own unique set of challenges, but it is an excellent tool to have in order to start or further your career as a web developer.

Is React still relevant 2022?

React Javascript is an indisputably great language for programming web pages and applications, and within that JS spectrum, it is still unchallenged. There are other players in the arena, but professional web app developers would confirm that React still remains on top of others in 2022.


2 Answers

Chrome doesn't let you load file:// urls via XHR (which as mentioned elsewhere is how the in browser transform works). You have a couple options:

  • Use a different browser. I know Firefox works.
  • Start a local web server (Python ships with one, so if you have that installed it's very simple - http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python).
  • Put the script inline instead of in a separate file. That's doable for something simple like this but you'll want to try one of the other options as your code gets more complicated.
like image 148
Paul O'Shannessy Avatar answered Sep 27 '22 20:09

Paul O'Shannessy


The following command works for me. But before the command, you may need to stop chrome process anyhow, can be from task manager.

 "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files --disable-web-security 

Second way, you can also use --allow-file-access-from-files flag in the chrome shortcut properties. But this is recommended only for developing purpose.

like image 41
Masud Shrabon Avatar answered Sep 27 '22 20:09

Masud Shrabon