Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js: data not getting populated

I'm doing the tutorial on the React.js website. Here is my code:

<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">
      /**
       * @jsx React.DOM
       */
      // The above declaration must remain intact at the top of the script.
      // Your code here
      var commentsData = [
        {author: "Pete Hunt", text: "This is one comment"},
        {author: "Jordan Walke", text: "This is *another* comment"}
      ];

      var CommmentBox = React.createClass({
        getInitialState: function() {
          return {data: []};
        },
        componentWillMount: function() {
          this.loadComments();
        },
        render: function() {
          return (
            <div className='commmentBox'>
              <h1>Comments</h1>
              <CommentList data={this.state.data} />
              <br />
              <CommentForm onCommentSubmit={this.handleCommentSubmit} />
            </div>
          ); 
        }, 
        handleCommentSubmit: function(comment) {
          commentsData.push(comment);
          this.loadComments();
        }, 
        loadComments: function() {
          console.log(commentsData.length);
          this.setState({data: commentsData});
        }
      });

      var CommentList = React.createClass({
        render: function() {
          var commentNodes = this.props.data.map(function(comment) {
            return <Comment author={comment.author} text={comment.text} />;
          });
          return (
            <div className='commentList'>
              {commentNodes}
            </div>
          );
        }
      });

      var CommentForm = React.createClass({
        render: function() {
          return (
            <form className='commentForm' onSubmit='handleSubmit'>
              <input type='text' placeholder='Your name' ref='author'/>
              <input type='text' placeholder='Your comment' ref='text'/>
              <input type='submit' value='Post' />
            </form>
          );
        },
        handleSubmit: function() {
          var author = this.refs.author.getDOMNode().value.trim();
          var text = this.refs.text.getDOMNode().value.trim();
          this.props.onCommentSubmit({author: author, text: text});
          this.refs.author.getDOMNode().value = '';
          this.refs.text.getDOMNode().value = '';
          return false;
        }
      });

      var Comment = React.createClass({
        render: function() {
          return(
            <div className='comment'>
            <br />
              <h3 className='commentAuthor'>
                {this.props.author} wrote:
              </h3>
              <h3 className='commentText'>
                {this.props.text}
              </h3>
            </div>
          );
        }
      });

       React.renderComponent(
        <CommmentBox />, 
        document.getElementById('content')
      );

    </script>
  </body>
</html>

When I add comments, they don't show up in the comments list. I'm logging the length of the comment array to the console, and it never changes. What am I doing wrong?

like image 595
tldr Avatar asked Feb 07 '14 22:02

tldr


People also ask

Why state is not updating immediately in React?

State updates in React are asynchronous; when an update is requested, there is no guarantee that the updates will be made immediately. The updater functions enqueue changes to the component state, but React may delay the changes, updating several components in a single pass.

How do I load a large data in ReactJS?

react-virtualized Go to your App component and paste the code below: import React from 'react'; import faker from 'faker' import { List } from "react-virtualized"; import './App. css'; function App() { const data = new Array(1000). fill().

Is React Still King?

React is still the best JavaScript framework to use for frontend web development in 2022, the framework features many out of the box tools that makes it a breeze to work with. React was created by Jordan Walke at Facebook in 2013. Since then react has grown to become the most used JavaScript framework right now.


1 Answers

You need to do this because you were using a string in onSubmit event.

<form className='commentForm' onSubmit={this.handleSubmit}>

You had this in your sample code:

<form className='commentForm' onSubmit='handleSubmit'>

Your code caused a Uncaught TypeError: string is not a function error. Because of that error it was not hitting the handleSubmit function and also caused the browser to reload.

like image 147
Gohn67 Avatar answered Oct 18 '22 03:10

Gohn67