Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!