More React - Basic Structure & Props
Daily Standup
March 04, 2018
Today I continued learning React in the class I’m doing. We’re using create-react-app
and I’m definitely feeling like a lot of the basics are being abstracted away, but I’ll go with it for now. Actually, it’s a lot of new ideas even with a lot of things that have been taken away, so maybe it’s a good introduction. We started by building a basic app and passing props around.
Basic App Structure
Based on what we’ve covered so far, the basic structure of an app is as follows:
index.js
uses React and ReactDOM to render your app file- The app file is itself a component which passes props on to its subcomponents. For example in our
RecipeApp.js
, we display theRecipe
component and put several details in:
class RecipeApp extends Component { |
- The
Recipe
component uses these props to render data:
class Recipe extends Component { |
- Everything is linked using
import
statements, for example:
import React, { Component } from "react"; |
defaultProps
The above example is very prescriptive but doesn’t handle optional data…but we can fix this!
class IngredientList extends Component { |
Setting default props ensures that something is available for the .map()
function above. Another option is to define the default props separately from the class:
class IngredientList extends Component { |
propTypes
propTypes
is a React package that sets requirements on component props. It’s not included in React and needs to be added separately: npm i prop-types
. It’s only useful in development and does nothing in production. Here’s the documentation for future reference.
Up Next
More React lessons!