Code up a Figma Design in React Native
In this guide, we’ll use React Native to recreate the design for a single todo item from Figma.
2. Clear out starter code
Section titled “2. Clear out starter code”With this structure in mind, let’s take a look at how we might build the component in React Native code.
-
Open the project in your code editor and run the app with the command below:
Terminal window npm run dev -
Decide if you want to see the app on your phone or in the browser by following the instructions in your terminal.
-
Open
app/index.tsxand replace all the code inside with the following:app/index.tsx import { View, Text } from "react-native";export default function HomeScreen() {return (<View className="flex flex-1 py-32 bg-background"><Text className="text-white text-center">Hello, world!</Text></View>);}This gives us a simple
Viewcomponent with aTextcomponent inside it:
3. Plan out the components
Section titled “3. Plan out the components”3. Style the todo item
Section titled “3. Style the todo item”With your pair programming partner, style the todo item to match the design.
Your final implementation should look and behave something like this:
-
Style the text inside the green
Viewcomponent to match the design. You will need to think about:- the font size
- the font colours (tip - use the
text-foregroundandtext-foreground-transparentclasses)
-
Add a checkbox in the pink
Viewcomponent.You already have a
checkboxcomponent in your project atcomponents/ui/checkbox.tsx.- Research how to import and use this by following the documentation.
-
Remove the placeholder borders from the
Viewcomponents and style them to match the design. You will need to think about:- the border along the bottom of the todo item (tip - use the
border-bandborder-foreground-transparentclasses where you feel is correct) - the padding and margins
- the border along the bottom of the todo item (tip - use the
Good luck!
import * as React from 'react';import { Checkbox } from '@/components/ui/checkbox';import { View, Text } from 'react-native';
export default function HomeScreen() { const [checked, setChecked] = React.useState(true);
return ( <View className="bg-background flex flex-1 py-32"> <View className="flex h-20 w-full flex-row border-2 border-cyan-400"> <View className="flex h-full w-24 border-2 border-pink-400"></View> <View className="flex h-full flex-1 border-2 border-green-400"> <Text className="text-foreground">Feed the cat</Text> </View> </View> </View> <View className="bg-background flex-1 items-center justify-center gap-5 p-6"> <View className="flex w-full flex-row"> <View className="flex h-full w-24 px-8 py-5"> <Checkbox className="border-foreground checked:bg-foreground" checked={checked} onCheckedChange={setChecked} /> </View> <View className="border-foreground-transparent flex h-full flex-1 gap-1 border-b py-4"> <Text className="text-foreground text-xl">My test item</Text> <Text className="text-foreground-transparent text-xl">Test category</Text> </View> </View> </View> );}4. Create a dummy ‘Task’
Section titled “4. Create a dummy ‘Task’”Inside app/index.tsx, add a single object called task to the HomeScreen component.
We will use this as a dummy task to test our new Task component.
export interface ITask { title: string; category: string; isChecked: boolean;}
export default function HomeScreen() { const [checked, setChecked] = React.useState(true);
const task: ITask = { title: "My test item", category: "Test category", isChecked: false, };
return ( <View className="bg-background flex-1 items-center justify-center gap-5 p-6">13 collapsed lines
<View className="flex w-full flex-row"> <View className="flex h-full w-24 px-8 py-5"> <Checkbox className="border-foreground checked:bg-foreground" checked={checked} onCheckedChange={setChecked} /> </View> <View className="border-foreground-transparent flex h-full flex-1 gap-1 border-b py-4"> <Text className="text-foreground text-xl">My test item</Text> <Text className="text-foreground-transparent text-xl">Test category</Text> </View> </View> </View> );}Notice how we’ve created a new ITask interface here and exported it from the app/index.tsx file?
This is so that we can import it into other files, which we will be doing later 😉.
5. A dedicated Task component file
Section titled “5. A dedicated Task component file”Challenge
Section titled “Challenge”Create a new file at components/Task.tsx and extract your todo item JSX code into a new component called Task.
- The new component should accept just one prop:
task(ITask)- You will need to import the
ITaskinterface fromapp/index.tsx
import React from 'react';import { View } from 'react-native';
import { Text } from '@/components/ui/text';import { Checkbox } from '@/components/ui/checkbox';import { ITask } from '@/app';
interface TaskProps { task: ITask;}
function Task({ task }: TaskProps) { const { title, category, isChecked } = task; const [checked, setChecked] = React.useState(isChecked);
return ( <View className="flex w-full flex-row"> <View className="flex h-full w-24 px-8 py-5"> <Checkbox className="border-foreground checked:bg-foreground" checked={checked} onCheckedChange={setChecked} /> </View> <View className="border-foreground-transparent flex h-full flex-1 gap-1 border-b py-4"> <Text className="text-foreground text-xl">{title}</Text> <Text className="text-foreground-transparent text-xl">{category}</Text> </View> </View> );}
export { Task };6. Import the ‘Task’ component
Section titled “6. Import the ‘Task’ component”Finally, update your HomeScreen JSX to use the new Task component.
Pass the task object as a prop to the Task component.
import * as React from 'react';import { Checkbox } from '@/components/ui/checkbox';import { View, Text } from 'react-native';import { View } from 'react-native';import { Task } from '@/components/Task';
export interface ITask { title: string; category: string; isChecked: boolean;}
export default function HomeScreen() { const [checked, setChecked] = React.useState(true);
const task: ITask = { title: 'My test item', category: 'Test category', isChecked: false, };
return ( <View className="bg-background flex-1 items-center justify-center gap-5 p-6"> <View className="flex w-full flex-row"> <View className="flex h-full w-24 px-8 py-5"> <Checkbox className="border-foreground checked:bg-foreground" checked={checked} onCheckedChange={setChecked} /> </View> <View className="border-foreground-transparent flex h-full flex-1 gap-1 border-b py-4"> <Text className="text-foreground text-xl">My test item</Text> <Text className="text-foreground-transparent text-xl">Test category</Text> </View> </View> <Task task={task} /> </View> );}::::
Summary
Section titled “Summary”In this guide, we have:
- Recreated the todo item design from the Figma file
- Created a new
Taskcomponent - Passed a
taskobject as a prop to theTaskcomponent
Next steps
Section titled “Next steps”In the next guide, we will learn how to edit a task via a dialogue box.