Skip to content

Instantly share code, notes, and snippets.

@lunaleaps
Last active February 29, 2024 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunaleaps/79bb6f263404b12ba57db78e5f6f28b2 to your computer and use it in GitHub Desktop.
Save lunaleaps/79bb6f263404b12ba57db78e5f6f28b2 to your computer and use it in GitHub Desktop.
Automatic Batching Example
import * as React from 'react';
import {SafeAreaView, ScrollView, Text, View} from 'react-native';
// Slider is a native component
import Slider from '@react-native-community/slider';
export default function ManyTiles(props) {
const colors = ['red', 'green', 'blue', 'orange'];
const views = [];
const [value, setValue] = React.useState(1);
for (let i = 0; i < value; ++i) {
const backgroundColor = colors[i % colors.length];
const size = 14;
const view = (
<View
key={i}
style={{
width: size,
height: size,
borderWidth: 0.5,
backgroundColor,
}}
/>
);
views.push(view);
}
return (
<SafeAreaView>
<Text style={{textAlign: 'center'}}>
Render <Text style={{fontWeight: 500}}>{value}</Text> Tiles
</Text>
<Slider
value={1}
minimumValue={1}
maximumValue={1000}
step={1}
onValueChange={newValue => {
// This update is now automatically batched in React 18
setValue(newValue);
}}
/>
<View
style={{
flexDirection: 'row',
flex: 1,
flexWrap: 'wrap',
}}>
{views}
</View>
</SafeAreaView>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment