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

93
Visualizações
condition to dispatch either one option or the other

I have to dispatch either the selectedMovie id or the selectedSerie id to redux. I am trying to make a condition. This is what I did now, I added a || operator but I don't quite know if that is possible.

useEffect(() => {
    if (isModalVisible) {
        if (Array.isArray(dataToSync) && dataToSync.length > 0) {
            dataToSync?.forEach((el) =>
                dispatch(syncToServer(language, el, SelectedMovie[0]?.id || 
SelectedSerie?.id))
            );
        }
    }
}, [dispatch, language, isModalVisible]);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes, use of || is very much possible. When you use The logical OR ( || ) operator, if the first evaluation of the first operand is false, the second operand is evaluated.

var a = (val) => console.log(val, "val")
x = {}
var e = 'e';
a(x?.ele || e);
a(x?.ele ? x?.ele : e);

Solution 2: ??

Nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

dispatch(syncToServer(language, el, SelectedMovie[0]?.id ?? SelectedSerie?.id))

Solution 3: ? :

Another way would be to use a ? : operator.

dispatch(syncToServer(language, el, SelectedMovie[0]?.id ? SelectedMovie[0].id : SelectedSerie?.id))
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