我是 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'));
我在 http://jsfiddle.net/rexonms/409d46av/ 中有 js fiddle
最佳答案
你快到了。请记住,JSX 中 { 和 } 中的所有内容都只是普通的 Javascript。要从 Javascript 中的函数获取返回值,您必须调用它。所以像这样(注意 this.func
之后的括号):
return (
<div>
<div>Props: {this.props.name}</div>
<div>Function: {this.func()}</div>
</div>
);
关于javascript - 如何从 React 组件中的 React 函数获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29690595/