As a first React-Native project, I'm just trying to create a simple animation. It's a ListView filled with squares that are animating their opacity. For that I've created a ListView that contains Square which is a 50X50 square painted with one of 3 colors.
class Square extends Component {
constructor(props) {
super(props);
_defaultTransition = 500;
this.state = {
_rowOpacity : new Animated.Value(0),
targetOpacity: 0
};
setInterval(() => {
this.state.targetOpcacity = this.state.targetOpcacity == 0 ? 1 : 0;
this.animate(this.state.targetOpcacity);
}, Math.random() * 1000);
}
animate(to) {
Animated.timing(
this.state._rowOpacity,
{toValue: to, duration : 500}
).start();
}
componentDidMount() {
this.animate(1);
}
render() {
var rand = Math.random();
var color = rand < 0.3 ? 'powderblue' : rand > 0.6 ? 'skyblue' : 'steelblue';
return (
<Animated.View
style={{height:50, width: 50, backgroundColor: color, opacity: this.state._rowOpacity}}>
{this.props.children}
</Animated.View>
);
}
}
class ReactFirst extends Component {
constructor(props) {
super(props);
var array = Array.apply(null, {length: 1000}).map(Number.call, Number);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(array)
};
}
render() {
return (
<ListView contentContainerStyle={styles.list}
dataSource={this.state.dataSource}
renderRow={(rowData) => <Square></Square>}
/>
);
}
}
var styles = StyleSheet.create({
list: {
flexDirection: 'row',
flexWrap: 'wrap'
}
});
The result is that only 11 squares are animating themselves on the screen, although the array has 1000 cells. this means, one and a half lines are rendered and the rest of the screen is blank.
I would like to have the entire screen filled with animating squares.
Thanks a lot for the help, Giora.
Ok, after trying some things and going back to the docs, I've found the: initialListSize prop which allows to set the initial number of rendered rows.
<ListView contentContainerStyle={styles.list}
dataSource={this.state.dataSource}
initialListSize={size}
renderRow={(rowData) => <Square></Square>}
/>
And it worked.