0

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' }
]

2
  • 2
    If you need to skip, you're looking for filter, not map. Commented Feb 8, 2022 at 17:47
  • They're also useful in conjunction - items.filter(shouldIKeep).map(renderJSX). Commented Feb 8, 2022 at 17:49

4 Answers 4

2

Try a conditional append.

const extendedTabs = [...modalTabs, ...(gradeIsMandatory ? [{ key: 'grade', title: 'Grade' }] : [] )];
Sign up to request clarification or add additional context in comments.

Comments

0

I would delete the useEffect and render this:

return (
    <View>
        <View>
            <View>
                {modalTabs.map((tab, i) => (
                    <TouchableOpacity>
                      <Text>{tab.title}</Text>
                    </TouchableOpacity>
                ))}
                {
                  gradeIsMandatory && <TouchableOpacity><Text>Grade</Text></TouchableOpacity>
                }
            </View>
        </View>
    </View>
);

Comments

0

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

Comments

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

2 Comments

This definitely works, but it's the derived state anti-pattern. Because the wanted output is deterministic, ie the result is always the same based on given inputs, a simple const statement is all you need. reactjs.org/blog/2018/06/07/…
I agree with you, in this case which the result is deterministic using a simple const at the top of the functional component is absolutely correct, my answer causes an extra render, thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.