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

176
Visualizações
How to set default params for data which comes from server?

I send a GET request and in response get a user object like this:

{
  name: 'Alex',
  email: 'test@test.com',
  age: 18,
}

then I need to set this object to the state and render data in some different components

interface User {
  name: string | null,
  email: string,
  age: number,
}

const [user, setUser] = useState<User>();

const getUser = async () => {
  const response = await API.getUserData();
  setUser(response);
} catch (error) {
  console.log(error)
}

The problem: For some reason name might be null and in this case I should render on a page default values. As far as I know it can be done in many ways. The most obvious one is with conditional statements like this:

<h1>{name ? name : 'Default value'}</h1>

or

<h1>{name ?? 'Default value'}</h1>

But for me its a bad option because too many conditions in many components. Is there a way to set a default values in getUser function? Eventually I want to have my components to be like this:

// if name is null then its equal to some default value otherwise it equals to the value from server.
<h1>{name}</h1>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think one option is transforming response before you invoke setUser. Something like this

function transformUserData(userData) {
  // make transformation and replacement on Default Values
}

const getUser = async () => {
  const response = await API.getUserData();
  const responseWithDefaultValues = transformUserData(response);

  setUser(responseWithDefaultValues);
} catch (error) {
  console.log(error)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Yes, you can set your default values in useState:

const [user, setUser] = setState<User>({
  name: "default value",
  email: "default email address",
  age: 0
})

Then with API calls, it gets changes.

But there is an issue if the API response was {name: null}, it's not back to default value. so you must destruct your response and check the values before setUser:

const getUser = async () => {
  const response = await API.getUserData();
  const {name, email, age} = response;
  setUser({name: name || "default value", email: email, age: age});
} catch (error) {
  console.log(error)
}

as a side note:

The proper syntax for short circuit is:

<div> {name && 'Default value'} </div>
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