Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Maximum Depth Exceeded Warning when using it #302

Open
anu1097 opened this issue Sep 25, 2024 · 3 comments
Open

Getting Maximum Depth Exceeded Warning when using it #302

anu1097 opened this issue Sep 25, 2024 · 3 comments

Comments

@anu1097
Copy link

anu1097 commented Sep 25, 2024

Here's my component -

import React, { memo, ReactElement, useEffect, useState } from 'react';
import { StyleSheet } from 'react-native';
import { Dropdown } from 'react-native-element-dropdown';

interface InputWithDropdownProps {
  field: string;
  icon: ReactElement<any, any>;
  values: { label: string; value: string }[];
}

const InputWithDropdown = memo(({ field, icon, values }: InputWithDropdownProps) => {
  const [value, setValue] = useState<string | null>(null);
  const [isFocus, setIsFocus] = useState(false);
  // Debug to see re-renders
  useEffect(() => {
    console.log('Value or Focus changed: ', { value, isFocus });
  }, [value, isFocus]);

  const handleFocus = () => {
    if (!isFocus) {
      setIsFocus(true); // Ensure this only triggers once
    }
  };

  const handleBlur = () => {
    if (isFocus) {
      setIsFocus(false); // Ensure this only triggers once
    }
  };

  const handleChange = (item: { value: React.SetStateAction<string | null> }) => {
    if (value !== item.value) {
      setValue(item.value); // Avoid redundant state updates
      setIsFocus(false); // Collapse dropdown
    }
  };

  return (
    <Dropdown
      style={[styles.dropdown, isFocus && { borderColor: 'blue' }]}
      placeholderStyle={styles.placeholderStyle}
      selectedTextStyle={styles.selectedTextStyle}
      inputSearchStyle={styles.inputSearchStyle}
      iconStyle={styles.iconStyle}
      data={values}
      search
      maxHeight={300}
      labelField="label"
      valueField="value"
      placeholder={field}
      searchPlaceholder="Search..."
      value={value}
      onFocus={handleFocus}
      onBlur={handleBlur}
      onChange={handleChange}
      onChangeText={(text) => {
        if (text !== '') {
          setValue(text);
        }
      }}
      renderLeftIcon={() => icon}
    />
  );
});

export default InputWithDropdown;

const styles = StyleSheet.create({
  dropdown: {
    flex: 1,
    borderColor: 'gray',
    borderWidth: 0.5,
    borderRadius: 8,
    paddingHorizontal: 2,
  },
  label: {
    position: 'absolute',
    backgroundColor: 'white',
    left: 22,
    top: 8,
    zIndex: 999,
    paddingHorizontal: 2,
    fontSize: 14,
  },
  placeholderStyle: {
    fontSize: 16,
  },
  selectedTextStyle: {
    fontSize: 16,
  },
  iconStyle: {
    width: 20,
    height: 20,
  },
  inputSearchStyle: {
    height: 30,
    fontSize: 16,
  },
});

I'm using it like this -

          <InputWithIcon
            field={detail}
            icon={options[detail].icon}
            values={options[detail].values}
          />

I'm getting this warning in console -

console.js:288 Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.
Screenshot 2024-09-25 at 11 39 27 AM

console.log('Value or Focus changed: ', { value, isFocus }); only prints when Dropdown is interacted with.
I have tried debugging it and managed to pinpoint it to using Dropdown component. Kindly assist.

@anu1097
Copy link
Author

anu1097 commented Sep 27, 2024

@hoaphantn7604 can you have a look ?

@Rythinx
Copy link

Rythinx commented Sep 28, 2024

I had the same issue, and for some reason it happens when you have an empty array.
I made a small patch in the module on line 150. Try adding it and patch-package the module.
*** JUST A HOTFIX FOR ME ***

    useEffect(() => {
      if (data && data.length > 0) {
        const filterData = excludeData(data);
        setListData([...filterData]);
        if (searchText) {
          onSearch(searchText);
        }
      }
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [data, searchText]);

@Rythinx
Copy link

Rythinx commented Sep 28, 2024

I had the same issue, and for some reason it happens when you have an empty array. I made a small patch in the module on line 150. Try adding it and patch-package the module. *** JUST A HOTFIX FOR ME ***

    useEffect(() => {
      if (data && data.length > 0) {
        const filterData = excludeData(data);
        setListData([...filterData]);
        if (searchText) {
          onSearch(searchText);
        }
      }
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [data, searchText]);

#304

I also made a hotfix for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants