Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
Proper way to read if else logic in conditional rendering ReactJS

So I'm just barely learning reactJS and got a little confused for reading an if else in the conditional rendering that I've been written. This is the code that I've wrote:

    this.setState({
      loading: true
    })
    axios('https://api.randomuser.me/?nat=USA&results=5').then(response => this.setState({
        users : response.data.results,
        loading : false
      }))
  }

  componentWillMount() {
    this.getUser()
  }

  render() {
    return (
      <div className="App">
          {!this.state.loading ?
          this.state.users.map(user =>
             <div>
               <h2>{`${user.name.first} ${user.name.last}`}</h2>
             </div>) : 'Loading'}
          {console.log(this.state.loading)}
      </div>
    );
  }

When the program is still reaching for the API data, a 'Loading' string will be displayed and when the program already get the API data, it will disappear and displayed the data instead

I'm doing trial and error for the part in render(). first I put this.state.loading without ! (false) and the app just displaying Loading all the way with no end. But when I put ! (false), the program works just fine.

So I got confused on how to read the if else statement in this occurence, like

If this.state.loading condition is false (with !), write the <h2>. Else display 'Loading' string

But the problem is, the condition of this.state.loading before the API data obtained is true, since I wrote it before in here:

    this.setState({
      loading: true
    })
    axios('https://api.randomuser.me/?nat=USA&results=5').then(response => this.setState({
        users : response.data.results,
        loading : false
      }))
  }

Then why the Loading is working? Please tell me how to actually read this logic, thank you before.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

this.state.loading == true ? console.log("still loading") : console.log("loaded"); 

you could check the value like this

try to change

!this.state.loading 

to

this.state.loading == true

so if the loading state is equal to true it will display "still loading", and if the loading state is set to false it means that the ajax request is done and it will display "loaded".

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda