Skip to content

Commit

Permalink
Add a demo for react compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
kadikraman committed Jul 12, 2024
1 parent ffef2fc commit b5d1c14
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/secretModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { theme } from "../theme";
import { ExpoImageDemo } from "@/components/ExpoImageDemo";
import ChangeAppIcon from "@/components/ChangeAppIcon";
import { ScrollView } from "react-native-gesture-handler";
import { ReactCompilerDemo } from "@/components/ReactCompilerDemo";

export default function SecretModal() {
const { width } = useWindowDimensions();
Expand Down Expand Up @@ -44,6 +45,7 @@ export default function SecretModal() {
little example of native image transitions.
</ThemedText>
<ExpoImageDemo />
<ReactCompilerDemo />
</ThemedView>
</ScrollView>
);
Expand Down
2 changes: 1 addition & 1 deletion components/ExpoImageDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const styles = StyleSheet.create({
},
section: {
padding: theme.space24,
marginBottom: 200,
marginBottom: theme.space24,
},
centered: {
textAlign: "center",
Expand Down
70 changes: 70 additions & 0 deletions components/ReactCompilerDemo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { StyleSheet, Switch } from "react-native";
import { ThemedText, ThemedView } from "./Themed";
import { useState } from "react";
import { theme } from "@/theme";

export function ReactCompilerDemo() {
const [value, setValue] = useState(false);

return (
<>
<ThemedText
style={styles.centered}
marginBottom={theme.space12}
fontWeight="bold"
fontSize={24}
>
React Compiler Demo
</ThemedText>
<ThemedView
darkColor="rgba(255,255,255,0.15)"
lightColor={theme.lightActiveContent}
style={styles.section}
>
<ThemedText style={{ marginBottom: theme.space8 }}>
<ThemedText fontWeight="bold">Without React Compiler:</ThemedText> the
value in RenderCounter increments with each toggle
</ThemedText>
<ThemedText style={{ marginBottom: theme.space24 }}>
<ThemedText fontWeight="bold">With React Compiler:</ThemedText> the
value in RenderCounter does not increment
</ThemedText>
<ThemedView
style={{
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
}}
>
<Switch
value={value}
onValueChange={(value) => setValue(value)}
trackColor={{ true: theme.colorReactLightBlue }}
/>
<RenderCounter />
</ThemedView>
</ThemedView>
</>
);
}

let numberOfRenders = 0;

function RenderCounter() {
numberOfRenders++;
return (
<ThemedView style={{ flex: 1, marginLeft: theme.space24 }}>
<ThemedText>Number of renders: {numberOfRenders}</ThemedText>
</ThemedView>
);
}

const styles = StyleSheet.create({
centered: {
textAlign: "center",
},
section: {
padding: theme.space24,
marginBottom: 200,
},
});

0 comments on commit b5d1c14

Please sign in to comment.