我正在尝试将一个数字传递给React组件,但它被解析为字符串(jsfiddle)。如何让React了解我正在传递一个号码?我应该将字符串转换为组件中的数字吗?
var Rectangle = React.createClass({
render: function() {
return <div>
<div>{this.props.intValue + 10}</div>
<div>{this.props.stringValue + 10}</div>
</div>;
}
});
React.render(<Rectangle intValue="10" stringValue="Hello" />
, document.getElementById('container'));
&#13;
<script src="https://facebook.github.io/react/js/jsfiddle-integration.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
&#13;
答案 0 :(得分:47)
似乎在整数(实际上也是字符串)的情况下,我应该通过{}
传递数字,如下所示:
React.render(<Rectangle intValue={10} stringValue={"Hello"} />, document.getElementById('container'));
答案 1 :(得分:2)
您可以使用:
<MyComponent param1={10} param2={20} />
或者:
<MyComponent {...{param1: 10, param2: 20}} />