2016-02-25 110 views
5

以下是我反应组件的一部分。我有一个名为daysUntil的道具进入这个包含一个数字的组件。在这个例子中它正在通过其导致fontWeight设置功能中的号码0返回700React Native - NSNumber不能转换为NSString

render: function() { 
    return (
     <Text style={this.style()}> 
     {this.props.day} 
     </Text> 
    ) 
    }, 
    style: function() { 
    return { 
     fontWeight: this.fontWeight() 
    } 
    }, 
    fontWeight: function() { 
    var weight = 7 - this.props.daysUntil; 
    return weight * 100; 
    } 

我得到以下错误:

JSON value '700' of type NSNumber cannot be converted to NSSTring.

我假设这是因为字体重量预计值为字符串格式。什么是适当的解决这个?

预先感谢您!

回答

13

在你fontWeight设置()函数

return weight * 100; 

也许就变成了:

var val= weight * 100; 
return val.toString(); 
2

fontWeight设置需要一个字符串值,而不是一个整数。

只要确保你返回一个字符串:

return (weight * 100).toString(); 

同时也要确保你的“体重”变量​​不等于零。

1

您可以使用StyleSheetreact-native模块,像这样:

import StyleSheet from 'react-native' 

// declare the styles using Stylesheet.create 
const myStyles = StyleSheet.create({marginTop:30}) 

//... some code inside render method 

<Text style={myStyles}> 
     This is an example 
</Text>