Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - NSNumber cannot be converted to NSString

Below is part of my react component. I have a props named daysUntil coming into this component which contains a number. In this example it is being pass the number 0 which results 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'm assuming this is because font-weight expects the value to be in string format. What's the proper fix for this?

Thank you in advance!

like image 577
Sohrab Hejazi Avatar asked Feb 25 '16 23:02

Sohrab Hejazi


3 Answers

In your fontWeight() function

return weight * 100;

maybe becomes:

var val= weight * 100;
return val.toString();
like image 101
Robert Moskal Avatar answered Nov 16 '22 08:11

Robert Moskal


I had a similar problem, where I was passing in an icon instead of a uri to an Image. The code was written to accept icon = 'path/to/icon':

<Image source={{ uri: icon }}>

but I was passing in icon = require('path/to/icon') and I had to switch the jsx to

<Image source={icon}>
like image 24
ehacinom Avatar answered Nov 16 '22 08:11

ehacinom


fontWeight requires a string value and not an integer.

Just make sure you return a string:

return (weight * 100).toString();

Make also sure that your "weight" variable is not equal to zero.

like image 3
Leon Avatar answered Nov 16 '22 06:11

Leon