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

142
Vistas
How to stop useSelector from firing when object is empty?

I'm making a simple web app that has nested objects and I noticed when I delete the members object, which contains the sections object, I get an error because useSelectors is looking for the members.

My question is how do I make a dynamic useSelector that will stop firing when the object it is looking for or the object it is nested in is empty or the object is deleted.

    const selectedTool = useSelector(state => state.dashboard.selectedTool)
    const i = parseFloat(selectedTool)
    const members = useSelector(state => state.dashboard.sheets[i].members)
    const selectedMember = useSelector(state => state.dashboard.selectedMember)
    const sections = useSelector(state => state.dashboard.sheets[i].members[selectedMember].sections)

This is what the members object looks like as well as the nested sections object.

members: {
          0: {
              memberId: '',
              materialId: '',
              sectionId: '',
              totalLengthOfMember: '',
              yAxisUnbracedLength: '',
              yAxisEffectiveLengthFactor: '',
              zAxisUnbracedLength: '',
              zAxisEffectiveLengthFactor: '',
              unbracedLengthLateralTorsional: '',
              lateralTorsionalModificationFactor: '',
              slendernessRatioInCompression: '',
              slendernessRatioInTension: '',
              sections: {
                  0: {
                      sectionId: '',
                      sectionShape: '',
                      sectionName: '',
                      sectionView: '',
                      sectionEdit: ''
                  }
              }
          }
        },

My goal is to allow the user to delete the members object. However when the user does that, the sections useSelector gives an error because it cannot find a member index.

How do I fix this? Thank you guys!

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

0

The solution is easy, don't render the component that uses that selector if there are no members.

Your reply will most likely be, "but all those useSelector's are in the same component"

to which I will answer:

Your component is too large, just make smaller, single responsibility ones and split the useSelector's amongst those.

Post your component code.

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