In this lab, you’ll continue work on programming and meet with your team.
Expo/ReactNative/React provides a number of ways to manage app data, including local and global state, Context, and reducers. This exercise focuses on Context.
We’ll explore the use of React Context in this exercise by refactoring an app that uses Expo route parameters into an app that uses Context. The app displays a list of items for sale and gives details on them (see the example on the right).
Download the items-app from items-app.zip,
unzip it into lab05/item-app
, get it to run,
and then do the following.
Modify the items-app to use React Context state management, following the three “steps" discussed in Passing Data Deeply with Context, but applying it, here, to route navigation. Notes:
Step 1: Create the Context — This step is already done for you. Notes:
context/ItemContext.tsx
,
which is fully implemented but isn’t
currently being used by the app.
ItemContextType
—
This context is a bit more complex than
the one in the guide tutorial; take note
of the two elements it bundles.ItemContext
(and its
accessor: useItemContext()
)
— This is bit more complex as
well; TypeScript will force us to use
the error-checking specified in the
accessor rather than using the context
object directly.
Step 3: Provide the Context — This step is easier, so we’ll do it next. Notes:
app/app/_layout.tsx
, use the
ItemProvider
to wrap both
the index and details screen components.
ItemProvider
—
We’re using this as our context
wrapper.Step 2: Use the Context — And now, we refactor the screen components to use the context as follows.
app/index.tsx
:
useItemContext()
accessor. Because you only need the
items here, use an object-destructuring
assignment:const { items } = useItemContext();
item.id
.
app/details.tsx
:
id
from
the search parameters.items
and
deleteItem
from the
context, using a destructuring
assignment similar to the one you used
above.
id
to find the selected
item in the items
list. Use
the list find function with a fallback
value as a default:const selectedItem: Item = items.find(item => item.id === id) || defaultItem;
deleteItem(selectedItem.id)
When you have the updated app running on your device, save a screen
shot of the main list with one or more of the items deleted (e.g.
the example on the right has only two remaining items listed), and
add a new lab05/README.md
that answers to the following
questions:
Submit this application by pushing it to your cs262
repo, including: lab05/item-app/
;
lab05/screenshot.png
; and lab05/README.md
.
Today, we focus on your domain model, but will update all your management, analysis and design materials.
Meet with your teammates to build a domain model for your project.
This domain model will serve as the blueprint for the database schedule you build later in the project.