Alex Yong
Alex Yong

Reputation: 7645

Render component using a function did not work?

Instead of throwing everything into one render function, I try to create function but it did not appear. What am I doing wrong?

var dashboard = React.createClass({

    render: function() {
        return (
           <div>
              <Datepicker /> 
              {this.renderDate()} //why this render nothing?
           </div>
        );
    }

    renderDate(){
      <Datepicker />
    }

});

module.exports = dashboard;

Upvotes: 1

Views: 42

Answers (2)

Robin Chen
Robin Chen

Reputation: 131

the function renderDate() is wrong and lacked with a , before renderDate(), should like this:

var dashboard = React.createClass({

    render: function() {
        return (
           <div>
              <Datepicker /> 
              {this.renderDate()}
           </div>
        );
    },

    renderDate(){
    return <Datepicker />;
    }

});

var Datepicker = React.createClass({

    render: function() {
        return (
           <div>
           Datepicker content
           </div>
        );
    }

});

Upvotes: 0

Facundo La Rocca
Facundo La Rocca

Reputation: 3876

Your renderDate method must return a component. Try this

renderDate(){
    return (
       <Datepicker />
    );
}

Upvotes: 2

Related Questions