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

118
Visualizações
React how to return a label message based on if else condition

quite a beginner here but I am struggling to get the following to work. I have an if else condition here that I want the label message to depend on but I am getting an error message Cannot find name 'message' What am I doing wrong? The if else condition is the following:

if(devEnv()) {
    const message = useMessageAsString(
      <FormattedMessage
        id="xxxxxx"
        defaultMessage="I've read and accept the <aTerms>Terms of Use</aTerms> and <aPrivacy>Privacy Policy</aPrivacy>"
        values={{
          aTerms: (msg: string) => (
            <a
              rel="noreferrer noopener"
              target="_blank"
              href={URL_TERMS_AND_CONDITIONS_DEV}
            >
              {msg}
            </a>
          ),
          aPrivacy: (msg: string) => (
            <a
              rel="noreferrer noopener"
              target="_blank"
              href={URL_PRIVACY_POLICY}
            >
              {msg}
            </a>
          ),
        }}
      />
    )
  } else {
    const message = useMessageAsString(
      <FormattedMessage
        id="xxxxx"
        defaultMessage="I've read and accept the <aTerms>Terms of Use</aTerms> and <aPrivacy>Privacy Policy</aPrivacy>"
        values={{
          aTerms: (msg: string) => (
            <a
              rel="noreferrer noopener"
              target="_blank"
              href={URL_TERMS_AND_CONDITIONS}
            >
              {msg}
            </a>
          ),
          aPrivacy: (msg: string) => (
            <a
              rel="noreferrer noopener"
              target="_blank"
              href={URL_PRIVACY_POLICY}
            >
              {msg}
            </a>
          ),
        }}
      />
    )
  }

And then in the return I want to do this:

<CheckBox
                  data-testid={'terms_and_privacy_policy'}
                  checked={formValues.accepted_terms_and_privacy_policy}
                  onChange={() => {
                    if (!isValid) {
                      triggerValidation()
                    }
                    onFormFieldChange(
                      'accepted_terms_and_privacy_policy',
                      !formValues.accepted_terms_and_privacy_policy
                    )
                  }}
                  label={message}
                />
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is that the variable message is only declared within a limited scope. If this code all lives within render() then try defining the message before checking the condition as a mutable var (or let) like this:

var message = "";
if (devEnv()) {
  message = useMessageAsString(
  ...
} else {
  message = useMessageAsString(
  ...
}

return (
 ...
 label={message}
)

Alternatively you can stick with const but use a ternary operator for the assignemnt:

const message = devEnv() ? versionA : versionB;

If, for some reason, you are using class based components, and the message is defined in another function from render then you will need to declare it as a field on the class.

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