Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
React.js/React-Native how to skip Map() iteration if condition is falsy?

I have this simple component in React-Native, My goal is to skip the iteration if the given argument gradeIsMandatory is false, it should only iterate over warranty and price, How can I achieve this result without affecting performance? I will take any advice thanks

import React, { useCallback } from 'react';
import { View, Text, SafeAreaView, SectionList, StyleSheet, Image, TouchableOpacity, Alert} from 'react-native';





export const ModalHeader: React.FC<{}> = () => {
    const gradeIsMandatory = false
    
    //code I tried to use
    useEffect(()=>{
        if(!gradeIsMandatory){
            let appendGrade =  { key: 'grade', title: 'Grade' }
            modalTabs.push(appendGrade)
        }
    },[])
    return (
        <View>
            <View>
                <View>
                    {modalTabs.map((tab, i) => (
                        <TouchableOpacity>
                          <Text>{tab.title}</Text>
                        </TouchableOpacity>
                    ))}
                </View>
            </View>
        </View>
    );
}

const modalTabs = [
    { key: 'warranty', title: 'Your Waranty' },
    { key: 'price', title: 'Your price' }
]

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Try a conditional append.

const extendedTabs = [...modalTabs, ...(gradeIsMandatory ? [{ key: 'grade', title: 'Grade' }] : [] )];
about 4 years ago · Juan Pablo Isaza Denunciar

0

You cannot skip a map iteration. You can however just return <Fragment /> if your map condition isn't met.

about 4 years ago · Juan Pablo Isaza Denunciar

0

this useEffect will run once just after the first render of the component, manipulating modalTabs there will do nothing becuase modalTabs is not a State of this component. the correct way is:

import { useState, useEffect } from "react";

const modalTabsInitialValue = [
  { key: "warranty", title: "Your Waranty" },
  { key: "price", title: "Your price" }
];

export default (props) => {
  const [modalTabs, setModalTabs] = useState(modalTabsInitialValue);

  useEffect(() => {
    if (props.gradeIsMandatory) {
      setModalTabs((prevModalTabs) => {
        const appendGrade = { key: "grade", title: "Grade" };
        return [...prevModalTabs, appendGrade];
      });
    }
  }, [props.gradeIsMandatory, setModalTabs]);

  return {
    ...
  }
};
  1. send the gradeIsMandatory as a property to this component
  2. define modalTabs as state
  3. use a useEffect hook with props.gradeIsMandatory as a dependency
  4. inside the useEffect based on the value of gradeIsMandatory create a new array and change the modalTabs state forcing the component to reRender

Update: this is not the best solution for this problem, the correct answer as windowsill said is a simple const: answer

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda