React / Rails : Append dynamically element to DOM

Adrien de Villoutreys

Currently following facebook tutorial on React (react_tuto). I don't understand how 2 components can communicate so that on "submit a comment button" it appends dynamically the "comment list".

currently, comment are created on server but appears on page only when page refreshed

how can the comment appear on submit button?

This i my AddComment component

 var AddComment = React.createClass({
  getInitialState: function(){
    return {
      content: this.props.content,
      adrien: "before"
    }
  },

  handleKeyUp: function(e) {
    this.setState({
      content: this.refs.addComment.getDOMNode().value,
    })
  },

  handleValidation: function() {
    var that = this
    $.ajax({
      type: "POST",
      data: {comment: { content: that.state.content } },
      url: Routes.create_comment_path({format: 'json'}),
      success: function(data) {
       that.setState({
         content: "",
         adrien: "after"
       })
      }
    })
  },

  render: function(){
    return (
      <div>
      <textarea onKeyUp={this.handleKeyUp} value={this.state.value} ref="addComment"></textarea>
      <button onClick={this.handleValidation}>submit</button>
      </div>
    )
  }

})

This is my CommentList component:

var CommentList = React.createClass({
  render: function() {
    return (
      <div>
        {this.props.comments.map(function(comment){
          return <CommentListElement key={comment.id} comment={comment} />;
        })}
      </div>
    );
  }
});
Rohan Pujari

You need a common parent component for communication between different components.

I have updated you example a bit to include common parent component CommentSystem

Note: I have removed ajax call to just show the communication between component.

Check below link.

https://jsfiddle.net/j4yk3pzc/15/

Extra Info:

In react we store states on parent component and pass them down to children. Along with state we also pass actions to manipulate data down to the children. When child component want's to update data passed to it from parent, then it fires the action passed from the parent. This is called Data down action up approach. Data is passed from parent to child to grandchild. While actions are propagated from grandchild to child to parent.

If you don't want to create the parent component then you can use some Publish / Subscribe or EventEmitter based system to communicate between children having no common parent.

Reference:

http://ctheu.com/2015/02/12/how-to-communicate-between-react-components/

Code:

var CommentSystem = React.createClass({
    getInitialState: function() {
    return {
       comments: []
    }
  },
  addComments: function(comment) {
    var comments = this.state.comments;
    comments.push(comment);
    this.setState({comments: comments})
  },
  render: function() {
    return (
      <div>
        <AddComment addComments={this.addComments}/>
        <CommentList comments={this.state.comments}/>
      </div>
      )
  }
})

var AddComment = React.createClass({
  getInitialState: function(){
    return {
      content: this.props.content,
      adrien: "before"
    }
  },

  handleKeyUp: function(e) {
    this.setState({
      content: this.refs.addComment.getDOMNode().value,
    })
  },

  handleValidation: function() {
    var that = this;
    this.props.addComments(this.state.content);
  },

  render: function(){
    return (
      <div>
      <textarea onKeyUp={this.handleKeyUp} value={this.state.value} ref="addComment"></textarea>
      <button onClick={this.handleValidation}>submit</button>
      </div>
    )
  }
})

var CommentList = React.createClass({
  render: function() {
    return (
      <div>
        {this.props.comments.map(function(comment){
          return <CommentListElement key={comment.id} comment={comment} />;
        })}
      </div>
    );
  }
});

var CommentListElement = React.createClass({
  render: function() {
    return (
     <div>{this.props.comment}</div>
     )
  }
})

React.render(<CommentSystem/>, document.getElementById('container'));

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to dynamically append an element to dom-if in Polymer?

From Dev

Dynamically append an element to an object

From Dev

Remove and append DOM element

From Dev

Rails 4: select DOM element with dynamically generated id

From Dev

React Native - Dynamically Add Element to DOM on button press

From Dev

Add event handler to React.DOM element dynamically

From Dev

How to append to dom in React?

From Dev

Angular: append html in dom element

From Dev

Angular: append html in dom element

From Dev

Append element in DOM from array

From Dev

Dynamically Selecting a Javascript DOM Element

From Dev

Creating dynamically DOM structure with prepend and append

From Dev

Create React element dynamically

From Dev

Create React element dynamically

From Dev

Convert dynamically created element to DOM element

From Dev

Append DOM element to the D3

From Dev

AngularJs directive to append and remove DOM element with function

From Dev

Append DOM element not working with jQuery selectable

From Dev

Angularjs - Inserting directives dynamically to dom element

From Dev

Jquery selector not behaving dynamically on added dom element

From Dev

How to append a child element to a root element by using DOM in android?

From Dev

jQuery plugin: .append() adds element to DOM but element is not visible?

From Dev

Insert HTML element into DOM with React

From Dev

Appending a new dom element in react

From Dev

How to append "Input Tags" into the DOM in Runtime Dynamically using Selenium JavascriptExecutor?

From Dev

How to append "Input Tags" into the DOM in Runtime Dynamically using Selenium JavascriptExecutor?

From Dev

How to append element to variable to be render in react

From Dev

append/insert a value/string to a list element dynamically in Python?

From Dev

append element to dynamically generated div using same classnames - Jquery

Related Related

  1. 1

    how to dynamically append an element to dom-if in Polymer?

  2. 2

    Dynamically append an element to an object

  3. 3

    Remove and append DOM element

  4. 4

    Rails 4: select DOM element with dynamically generated id

  5. 5

    React Native - Dynamically Add Element to DOM on button press

  6. 6

    Add event handler to React.DOM element dynamically

  7. 7

    How to append to dom in React?

  8. 8

    Angular: append html in dom element

  9. 9

    Angular: append html in dom element

  10. 10

    Append element in DOM from array

  11. 11

    Dynamically Selecting a Javascript DOM Element

  12. 12

    Creating dynamically DOM structure with prepend and append

  13. 13

    Create React element dynamically

  14. 14

    Create React element dynamically

  15. 15

    Convert dynamically created element to DOM element

  16. 16

    Append DOM element to the D3

  17. 17

    AngularJs directive to append and remove DOM element with function

  18. 18

    Append DOM element not working with jQuery selectable

  19. 19

    Angularjs - Inserting directives dynamically to dom element

  20. 20

    Jquery selector not behaving dynamically on added dom element

  21. 21

    How to append a child element to a root element by using DOM in android?

  22. 22

    jQuery plugin: .append() adds element to DOM but element is not visible?

  23. 23

    Insert HTML element into DOM with React

  24. 24

    Appending a new dom element in react

  25. 25

    How to append "Input Tags" into the DOM in Runtime Dynamically using Selenium JavascriptExecutor?

  26. 26

    How to append "Input Tags" into the DOM in Runtime Dynamically using Selenium JavascriptExecutor?

  27. 27

    How to append element to variable to be render in react

  28. 28

    append/insert a value/string to a list element dynamically in Python?

  29. 29

    append element to dynamically generated div using same classnames - Jquery

HotTag

Archive