Welcome to this crash course on how to create a mobile app with React Native! Today's lab has three parts. We're gonna make a UberEats / DoorDash / Postmates / GrubHub clone!
- Fork this repo!
- Git clone the forked repo.
- In your terminal, run
yarn install
- Then launch the project with
expo start
There are some TODO
comments in the code to help you along. Feel free to delete them when you're done with the task.
We're gonna spruce up out MenuItem
component by using props! Recall that we can access props using the props
variable that gets passed into our component function. We access a specific prop by doing props.propName
. And the props are passed into our component by doing <ComponentName propName={propValue} />
.
Top Tip: When you want to use JavaScript variables or code within JSX code, you need to put it inside curly braces {}
https://reactnative.dev/docs/intro-react#custom-components
https://reactnative.dev/docs/intro-react#props
- Update the props in the
<MenuItem ... />
component inApp.js
with a new name, price, and image source. Remember that image source has to be usingrequire(...)
or{uri: "..."}
. NOTE: you won't see any changes yet. We still need to update our component to use these props... - Go to
MenuItem.js
. Update the FOOD NAME to use thename
prop. - Update the price to use the
price
prop - Update the image source to use the
imageSource
prop
Amazing! Look at that delicious food 😋.
Every React component has the ability to keep track of its own state. We're gonna keep track of the desired quantity for each menu item.
https://reactnative.dev/docs/intro-react#state
- First things first, let's go to
App.js
make another<MenuItem ...>
! You can copy and paste, and change the name, price, and image source to another food item. Wow! So easy! Hooray for reusable components! - Now let's go back to
MenuItem.js
and look at theuseState(0)
. Discuss with your partner: what does this line do? Check out the link above for more info. - In the "plus button"
onPress
function, try doingquantity++
to increase the quantity state directly. What goes wrong? Discuss with dat partner of yours! - Now try again with
setQuantity()
. Yay! Why does this work? Why do we need to do this? #DISCUSS - Update the
onPress
function for the "minus" button to decrease the quantity. Don't let the quantity go negative!
Now play around with those buttons! Notice how each menu item keeps track of its own quantity. Hooray for reusable components, again!
Last but not least, we're gonna add the ability for “Special Instructions” in case you want extra cheese, or less ice!
https://reactnative.dev/docs/textinput
- Make a new state variable for the special instructions text
- Use that new state variable in the “Special Instructions: {}”
<Text>
component - Update that state variable in the
onSubmitEditing
function of the<TextInput>
component
You did it!! A nice custom component, complete with multiple props and multiple state variables!
- Do some fancy styling! Make that main page and those menu items look ~~~beautiful ✨
- Add another prop to
MenuItem
! - Make another custom component from scratch!