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

139
Vistas
How do I change the state present in a specific item within a flatlist?

I am generating a flatlist that contains comments. Inside the comment component, I'm using a state isCollpsed to determine if the individual comment is collapsed or not. Pressing on each individual comment does make it collapse. However, I want to manipulate this state from the parent component without affecting every other comment. How could I achieve this?

I tried using the reference hook to access each individual item in the flatlist but it keeps returning 'undefined'. I'm using the react-native-collapsible library to collapse the comments.

My Flatlist:

 <FlatList
                    data={SAMPLE_COMMENTS}
                    keyExtractor={keyExtractor}
                    renderItem={({item})=>
                    
                    
                    <Comment 
                                ref={(el) => {rowRefs.current[item.id] = el} } 
                                onPress={()=>{rowRefs.current[item.id].collapseFunction()}}
                                body={item.body} 
                                author={item.author} 
                                level={item.level} 
                                createdAt={item.createdAt} 
                                commentId={item.id} 
                                commentChildren={item.replies} />}
                
                />

Comment Component :

const [isCollapsed, setIsCollapsed] = useState(false);
const collapseFunction = () => {setIsCollapsed(!isCollapsed)};



return (
    <Collapsible  collapsed={isCollapsed}>
    
        <TouchableWithoutFeedback onPress={onPress}>
            <View style={styles.container}>
        

            </View>
        </TouchableWithoutFeedback>

    </Collapsible>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you can use recursive function

// add this to parent
<MapComments
            comments={SAMPLE_COMMENTS}
            childClickHandler={onItemClickHandler}
          />

// MapComments component
const MapComments= ({
  Comments= [],
  childClickHandler,
}) => {
  return (
    <ScrollView>
      <Tree
        CommentTree={CommentTree}
        childClickHandler={childClickHandler}
      />
    </ScrollView>
  );
};

const Tree = ({CommentTree= [], childClickHandler}) => {
  return (
    <View>
      {CommentTree.map(tree => (
        <TreeNode
          key={tree.commentId}
          node={tree}
          childClickHandler={childClickHandler}
        />
      ))}
    </View>
  );
};

const TreeNode = ({node, childClickHandler}) => {
  const [childVisible, setChildVisiblity] = useState(false);

  const hasChild = node.commentChildren.length > 0 ? true : false;

  return (
    <View
      style={{marginRight: node.Level > 1 ? 40 : null}}>
      <TouchableOpacity
        onPress={() =>
          hasChild ? setChildVisiblity(prev => !prev) : childClickHandler(node)
        }>
        <Text numberOfLines={1} style={styles.label}>
          {node.body}
        </Text>
        {hasChild ? (
          <AntDesign name={childVisible ? 'minus' : 'plus'}
          />
        ) : (
          <FontAwesome name="circle" />
        )}
      </TouchableOpacity>

      {hasChild && childVisible && (
        <Tree
          childClickHandler={childClickHandler}
          knowledgeTree={node.commentChildren}
        />
      )}
    </View>
  );
};
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