将数字传递给组件 [英] Passing a number to a component

查看:46
本文介绍了将数字传递给组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个数字传递给 React 组件,但它被解析为字符串 (jsfiddle).我如何让 React 理解我正在传递一个数字?我应该将字符串转换为组件中的数字吗?

var Rectangle = React.createClass({渲染:函数(){返回 

<div>{this.props.intValue + 10}</div><div>{this.props.stringValue + 10}</div>

;}});React.render(<Rectangle intValue="10" stringValue="Hello"/>, document.getElementById('container'));

<script src="https://facebook.github.io/react/js/jsfiddle-integration.js"><;/脚本><div id="容器"><!-- 此元素的内容将替换为您的组件.-->

解决方案

似乎在整数(实际上也适用于字符串)的情况下,我应该像这样通过 {} 传递数字:

React.render(<Rectangle intValue={10} stringValue={"Hello"}/>, document.getElementById('container'));

I am trying to pass a number to a React component but it is getting parsed as string (jsfiddle). How do I make React understand that I am passing a number? Should I cast the string to a number in the component?

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'));

<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>

解决方案

It seems that in case of integers (and actually for strings as well) I should be passing the numbers via {} like so:

React.render(<Rectangle intValue={10} stringValue={"Hello"} />, document.getElementById('container'));

这篇关于将数字传递给组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆