0
I have the code:
import React, { Component } from 'react';
import { ScrollView } from 'react-native';
import { Tile, List, ListItem } from 'react-native-elements';
class UserDetail extends Component {
constructor(props){
super(props);
this.state = {
title: '',
phone: '',
}
}
render() {
const { id, title, whatsapp, email, phone, login, location } = this.props.navigation.state.params;
//RECEBO A CONSTANTE ACIMA DA TELA ANTERIOR. ALGUNS ITENS VEM COM INFORMAÇÃO, OUTROS NÃO
if (title){
this.setState({title: {title}});
}
if (phone){
this.setState({phone: {phone}});
//QUERO SABER SE A CONSTANTE ACIMA TEM INFORMAÇÃO, E ASSIM, USÁ-LA NO ESTADO CORRESPONDENTE, POIS QUANDO ESTÁ VAZIA DÁ ERRO
}
return (
<ScrollView>
<Tile
imageSrc={{ uri: `https://buscafree.com.br/assets/img/items/${id}.jpg`}}
featured
title={title}
//caption={email}
/>
<List>
<ListItem
title="Endereço"
rightTitle={this.state.title}
hideChevron
/>
<ListItem
title="Endereço"
rightTitle={this.state.phone} //USAREI AQUI
hideChevron
/>
</List>
</ScrollView>
);
}
}
export default UserDetail;
My code gets some information from the previous screen. However, not all items have a value (in my case, the PHONE item sometimes comes with information and sometimes not...)
When I try to print it on the screen, it gives error if it is empty/ null.
How do I check if it has information before printing on the screen?
Thanks for the tip. I am learning React-Active for about 20 days, it seems very easy, but has these details... can explain better why I should not use setstate in the render? what I want is that, if there is a phone, be created a listitem tag with the data. I’ll see if I do here. thanks
– Italo Rodrigo
One way to explain this is the "separation of concepts". The role of the method
render()
is to define the user interface model. Only this. How things appear. The interface interaction and modification medium is separated from the view. The official React documentation states: "The render() Function should be Pure, meaning that it does not Modify Component state, it Returns the same result each time it’s Invoked, and it does not directly Interact with the browser." https://reactjs.org/docs/react-component.html#render– nbkhope