Eu sunt încercarea de a schimba culoarea de o componentă în funcție de tipul de pokemon, care este afișat. Am acest punct de vedere în interiorul unui părinte componentă care retrievs pokemon de la un api. La pokemonType variabilă este de api. Se înregistrează și nu se poate consola.log('pokemonType') care înregistrează tipul de pokemon e.g "iarbă". Se pare că operatorul ternar nu este înregistrarea pokemonType și merge direct la default. Am scris acest lucru greșit?
{/* Pokemon Type */}
<View style={[
(pokemonType === 'grass') ? styles.grass : styles.pokemonTypeDefault,
(pokemonType === 'fire') ? styles.fire : styles.pokemonTypeDefault,
(pokemonType === 'water') ? styles.water : styles.pokemonTypeDefault,
(pokemonType === 'bug') ? styles.bug : styles.pokemonTypeDefault,
(pokemonType === 'ghost') ? styles.ghost : styles.pokemonTypeDefault,
(pokemonType === 'rock') ? styles.rock : styles.pokemonTypeDefault,
(pokemonType === 'steel') ? styles.steel : styles.pokemonTypeDefault,
(pokemonType === 'electric') ? styles.electric : styles.pokemonTypeDefault,
]}>
<Text style={styles.pokemonTypeText}>{pokemonType.toUpperCase()}</Text>
</View>
const styles = StyleSheet.create({
grass: {
backgroundColor: '#00FF00',
width: 250,
height: 30,
marginTop: 10,
marginLeft: 80,
borderRadius: 50,
},
fire: {
backgroundColor: '#FFA500',
width: 250,
height: 30,
marginTop: 10,
marginLeft: 80,
borderRadius: 50,
},
}) ..//All the other type styles
pokemonTypeDefault: {
width: 250,
height: 30,
marginTop: 10,
marginLeft: 80,
borderRadius: 50,
backgroundColor: 'blue',
},
Multumesc mult