React Native - NSNumber cannot be converted to NSString

The following is part of my reactive component. I have a props named daysUntil included in this component that contains a number. In this example, the number 0 is passed, resulting in the fontWeight function returning 700

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

I get the following error:

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

I assume this is because font-weight expects the value to be in string format. What is the correct fix for this?

Thank you in advance!

+5
source share
3 answers

In your function fontWeight ()

 return weight * 100; 

may be:

 var val= weight * 100; return val.toString(); 
+13
source

fontWeight requires a string value, not an integer.

Just return the line:

 return (weight * 100).toString(); 

Also, make sure your weight variable is not zero.

+2
source

You can use the StyleSheet module from react-native , something like this:

 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> 
+1
source

Source: https://habr.com/ru/post/1243900/


All Articles