How to know if constant has a value?

Asked

Viewed 724 times

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?

1 answer

1


Just check how Voce is doing. But Voce should not use setState() in the way render() -- this same should only have the role of model for sight, nothing more.

Then Voce must show a pro user interface in case the variable has no value. For example, if phone is not set, Voce shows a message saying something like "there is no phone value set".

Then after the value of the variable is set (I hope!) somehow, (e.g. click a button that makes an http request that at the end arrow the state of the component) the render method will skip the "if" and go directly to the desired view.

render() {
  if (!phone) {
    return <Text>Ainda nao ha informacoes do telefone. Carregando ...</Text>;
  }

  // se phone estiver definido, mostramos a visao desejada
  return (
    {/* Visao desejada */}
  );
}

It is worth pointing out that Voce does not need to ometer any screen only because of an undefined variable. In this case, Voce divides the interface into several components of presentation. Each of these components checks the values of the variable they need to present the information. If the value has not yet been set, each component shows its own "spinner" as the load message.

  • 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

  • 1

    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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.