最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - React-Native: FlatList re-rendering every single item - Stack Overflow

matteradmin5PV0评论

So I have a FlatList that gets fed an array of items. When I scroll to the bottom, I append more items to the end of that array and show to the user.

The issue is every single is item is rendered when we add to our item array, and sometimes even rendered twice.

Code is simplified here. /r/reactnative was unable to answer this question.

constructor(props) {
super(props);
this.state = {itemsTest: ['A', 'A', 'A', 'A']}
}

render() {


// Key is fine, since none of my items are changing indexes. I am just adding new items.
return (

<FlatList

keyExtractor={(item,index) => index}

scroll

data={this.state.itemsTest}

renderItem={({item, index}) => <View style={{width: windowWidth}}><Text>{item}</Text></View>

onEndReached={() => this.nextItemsTest()}

onEndReachedThreshold={0.2}

</FlatList>
)
}







nextItemsTest() {

// From suggestions below, just add an element to this array.

console.log('nextItemsTest');

const x = ['A'];

// Have worked with many variations of setting state here. I don't think this is the issue.
this.setState((prevState) => ({itemsTest: [...prevState.itemsTest, ...x],}));}

Here's the output. Every single item is re-rendered (twice even) every time my state is set.

I just want to re-render the items that haven't changed. Thank you.

So I have a FlatList that gets fed an array of items. When I scroll to the bottom, I append more items to the end of that array and show to the user.

The issue is every single is item is rendered when we add to our item array, and sometimes even rendered twice.

Code is simplified here. /r/reactnative was unable to answer this question.

constructor(props) {
super(props);
this.state = {itemsTest: ['A', 'A', 'A', 'A']}
}

render() {


// Key is fine, since none of my items are changing indexes. I am just adding new items.
return (

<FlatList

keyExtractor={(item,index) => index}

scroll

data={this.state.itemsTest}

renderItem={({item, index}) => <View style={{width: windowWidth}}><Text>{item}</Text></View>

onEndReached={() => this.nextItemsTest()}

onEndReachedThreshold={0.2}

</FlatList>
)
}







nextItemsTest() {

// From suggestions below, just add an element to this array.

console.log('nextItemsTest');

const x = ['A'];

// Have worked with many variations of setting state here. I don't think this is the issue.
this.setState((prevState) => ({itemsTest: [...prevState.itemsTest, ...x],}));}

Here's the output. Every single item is re-rendered (twice even) every time my state is set.

I just want to re-render the items that haven't changed. Thank you.

Share Improve this question edited Aug 26, 2020 at 13:30 Andrew Young asked Aug 26, 2020 at 13:22 Andrew YoungAndrew Young 7442 gold badges14 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Instead of Using View directly in your flatlist render you can create another ponent which is a pure ponent. so it will only re-renders when its data changes . e.g For your case it re-renders each item only once.

here is the solution

1st create a pure ponent like this

class SmartView extends PureComponent {
  render() {
    const {item, index} = this.props;
    return (
      <View style={{height: 300}}>
        {console.log('rendering', index)}
        <Text>{item}</Text>
      </View>
    );
  }
}

and then replace View with SmartView in your flatlist like this

 <FlatList
        keyExtractor={(item, index) => index.toString()}
        data={this.state.itemsTest}
        renderItem={({item, index}) => <SmartView item=                                
                                        {item} index={index} />}
        onEndReached={() => this.nextItemsTest()}
        onEndReachedThreshold={0.2}
      />

Post a comment

comment list (0)

  1. No comments so far