Last active
August 13, 2017 06:58
-
-
Save gastonmorixe/fb45ded04ff60a420ee09802e569fe0a to your computer and use it in GitHub Desktop.
RN 0.47.1 VirtualizedList infinite rerender Bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { PureComponent } from 'react' | |
import { | |
View, | |
Text, | |
VirtualizedList | |
} from 'react-native' | |
class DataSource { | |
getElementAtIndex (index) { | |
return { key: index } | |
} | |
} | |
const data = new DataSource() | |
function getItem (data, index) { | |
return data.getElementAtIndex(index) | |
} | |
function getItemCount (data) { | |
return 1 * 1000 * 1000 | |
} | |
function getRandomIntInclusive (min, max) { | |
min = Math.ceil(min) | |
max = Math.floor(max) | |
return Math.floor(Math.random() * (max - min + 1)) + min // The maximum is inclusive and the minimum is inclusive | |
} | |
// function getItemLayout (data, index) { | |
// return { length: 400, offset: 0, index: index } | |
// } | |
function randomBackground () { | |
const colors = ['red', 'green', 'blue', 'white', 'lime', 'pink'] | |
return colors[getRandomIntInclusive(0, colors.length - 1)] | |
} | |
class MyItem extends PureComponent { | |
render () { | |
const {item} = this.props | |
return ( | |
<View style={{ | |
borderWidth: 1, | |
borderColor: 'white', | |
backgroundColor: randomBackground() | |
}}> | |
<Text style={{fontSize: 100}}>{item.key}</Text> | |
<Text>{+(new Date())}</Text> | |
{ Array(getRandomIntInclusive(1, 10)).fill(0).map((el, i) => <Text key={i}>Hola {i}</Text>)} | |
</View> | |
) | |
} | |
} | |
const MyList = (props) => { | |
return ( | |
<VirtualizedList | |
data={data} | |
style={{backgroundColor: 'red'}} | |
// initialNumToRender={20} | |
// maxToRenderPerBatch={} | |
// windowSize={21} | |
// getItemLayout={getItemLayout} | |
getItemCount={getItemCount} | |
getItem={getItem} | |
keyExtractor={(item, index) => { | |
return item.key | |
}} | |
renderItem={({ item, index }) => { | |
return ( | |
<MyItem index={index} item={item} /> | |
) | |
}} | |
/> | |
) | |
} | |
export default MyList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment