如何从 React 组件中的 React 函数获取值
How to get value from a react function within a react component
我是 Reacj.js 的新手,无法从函数中取回值。我不确定我是否做对了。我需要函数表达式 "func" 到 return "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'));
中有 js fiddle
你快到了。请记住,JSX 中 { 和 } 中的所有内容都是常规的 Javascript。要从 Javascript 中的函数获取 return 值,您必须调用它。所以像这样(注意 this.func
之后的括号):
return (
<div>
<div>Props: {this.props.name}</div>
<div>Function: {this.func()}</div>
</div>
);
我是 Reacj.js 的新手,无法从函数中取回值。我不确定我是否做对了。我需要函数表达式 "func" 到 return "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'));
中有 js fiddle
你快到了。请记住,JSX 中 { 和 } 中的所有内容都是常规的 Javascript。要从 Javascript 中的函数获取 return 值,您必须调用它。所以像这样(注意 this.func
之后的括号):
return (
<div>
<div>Props: {this.props.name}</div>
<div>Function: {this.func()}</div>
</div>
);