Check if value is undefined before render React Native

I am using (Drop Down Picker library) to display categories of my data which I get from api call. I am waiting for api to fetch the data and then plan to display the categories. The problem is that I need to reformat data first and use hooks before render, but I can’t get it right.

how does the data for picker looks like:

    {label: 'USA', value: 'usa'},
    {label: 'UK', value: 'uk'},
    {label: 'France', value: 'france'/>},

my hooks:

    const [country, setCountry] = useState("0");
    const [categoryData, setCategoryData] = useState();
    const [testingCategories, setTestingCategories] = useState({
        label: null,
        value: null,

my effect hooks and reformatting the data:

//this hook calls api and sets the data to categories data of my type
useEffect(() => {
    getData(api.API_GET_DEPS, setIsLoading, setCategoryData);
}, []);

//this hooks reformats the data into acceptable format for picker
useEffect(() => {
            ? => ({
                    label: item.DEPNAME, //label
                    value: item.DEPID,   //value
            : [{ label: null, value: null }]
}, [categoryData]);

my render of drop down:

<View style={styles.container}>
                    {testingCategories ? (
                            containerStyle={{ height: 50 }}
                            style={{ backgroundColor: "#fafafa" }}
                                justifyContent: "flex-start",
                            dropDownStyle={{ backgroundColor: "#fafafa" }}
                            onChangeItem={(item) => {
                            onOpen={() => setContainerOpacity(0.1)}
                            onClose={() => setContainerOpacity(1)}
                                fontSize: 16,
                                color: colors.dark,
                                fontWeight: "600",
                    ) : (

I get an error that drop down picker can’t match the defaultValue with any label in testingCategories because it is null and haven’t loaded yet. I suppose I am using setter wrong because I can’t check whether testingCategories‘s first element was loaded. What am I doing wrong?

165 thoughts on “Check if value is undefined before render React Native”

  1. 26648 399130Youre so correct. Im there with you. Your blog is surely worth a read if anyone comes throughout it. Im lucky I did because now Ive obtained a entire new view of this. I didnt realise that this problem was so critical and so universal. You totally put it in perspective for me. 482931