如何从反应组件中的反应函数中获取价值

rex*_*rex 6 javascript reactjs

我是Reacj.js的新手,无法从函数中获取价值.我不确定我做得对.我需要函数表达式"func"来返回"from func"并替换{this.func}.不确定我错过了什么.

var Hello = React.createClass({

    func: function(){
        return 'from func';
    },

    render: function() {
        return <div>
                    <div>Props: {this.props.name}</div>
                    <div>Function: {this.func}</div>
               </div>;
    }
});

React.render(<Hello name="from props" />, document.getElementById('container'));
Run Code Online (Sandbox Code Playgroud)

我在http://jsfiddle.net/rexonms/409d46av/中有js小提琴

And*_*ahl 26

你快到了.请记住,JSX中{和}内的所有内容都只是常规的Javascript.要从Javascript中的函数获取返回值,您必须调用它.所以这样的事情(注意后面的parens this.func):

return (
  <div>
    <div>Props: {this.props.name}</div>
    <div>Function: {this.func()}</div>
  </div>
);
Run Code Online (Sandbox Code Playgroud)