2016-02-25 23 views
5

Di seguito è parte del mio componente di reazione. Ho degli oggetti di scena chiamati giorni. Non arrivo in questo componente che contiene un numero. In questo esempio è l'essere passare il numero 0 che determina la funzione fontWeight ritorno 700React Native - NSNumber non può essere convertito in 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; 
    } 

ottengo il seguente errore:

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

sto assumendo questo è perché font-weight aspetta che il valore per essere in formato stringa. Qual è la correzione corretta per questo?

Grazie in anticipo!

risposta

13

Nella funzione

return weight * 100; 

fontWeight() diventa forse:

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

fontWeight richiede un valore stringa e non un numero intero.

Basta fare in modo di tornare una stringa:

return (weight * 100).toString(); 

anche assicurarsi che la variabile "peso" non è uguale a zero.

1

È possibile utilizzare StyleSheet da react-native modulo, qualcosa di simile a questo:

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