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

223
Vistas
useState is not updating even after the value that trigger the update changes React Native

I am trying to append object into Firestore. The method of adding into Firestore is by checking the key of the object. For instance, 'Electronics1' is the key of object that is present in the document, then increase the last digit by 1 and append into the document, which will be 'Electronics2'. My problem is that i use setIndex useState to record the largest digit in the document. However, for the first time, the index will always be 0 even when the 'value' changes. How can I make it to update whenever the value in the dropdown changes?

const [index, setIndex] = useState(0);
useEffect(() => {
    let isCancelled = false;
    async function dataToAdd() {
      if (value !== null) {
        const charLen = value.length;
        const docRef = doc(db, 'users', auth.currentUser.uid, 'expense', value);
        const docSnap = await getDoc(docRef);
        if (docSnap.exists()) {
          const singDocRec = docSnap.data();
          let num = 0;
          Object.entries(singDocRec).map(([key, record]) => {
            if (key != null || key != '' || key != undefined) {
              let i = key.slice(charLen, key.length);
              if (i > num) {
                num = i;
              }
            }
          });
          if (!isCancelled) {
            setIndex(parseInt(num) + 1);
          }
        }
      }
    }
    dataToAdd();
    return () => {
      isCancelled = true;
    };
  }, [value]);

const createObj = value => {
    const concatStr = value.concat(index.toString());
    let obj = {};
    obj[concatStr] = {title: title, date: date, category: value};
    return obj;
  };

<DropDownPicker
          style={{
            borderRadius: 0,
          }}
          open={open}
          value={value}
          items={items}
          setOpen={setOpen}
          setValue={setValue}
          setItems={setItems}
        />
<TouchableOpacity
        onPress={() => {
          addFunc(value, createObj(value));
          navigation.navigate('Home');
        }}
        style={{
          width: 300,
          height: 50,
          marginTop: 20,
          borderWidth: 2,
          justifyContent: 'center',
        }}>
        <Text style={{textAlign: 'center', color: 'black', letterSpacing: 2}}>
          CONFIRM
        </Text>
      </TouchableOpacity>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Why not try to call all the function inside your useEffect in the onChangeValue or onSelectItem ?onChangeValue={(value) => { callTheFunctionhere() }}

about 4 years ago · Juan Pablo Isaza Denunciar

0

The strick inequality operator is written !== in JavaScript :

Try : if (key !== null || key !== '' || key !== undefined)

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