JS-201-ReactFundamentals
  • Part 0: App Overview
  • Part 1: Intro to React
    • 1.0: Packages Setup
    • 1.1: Project Setup
    • 1.2: React Router Dom
  • Part 2: JavaScript Concepts
    • 2.0: ES6 Overview
    • 2.1: classes
    • 2.2: constructors
    • 2.3: extends
    • 2.4: super
    • 2.5: interpolation
    • 2.6: map
    • 2.7: filter
  • Part 3: Functional Components
    • 3.0: Functional Components Overview
    • 3.1: Calling Functional Components
    • 3.2: Single Element Rule
    • 3.3: Arrow Functions
    • 3.4: Challenge
    • 3.4: Solution
    • 3.5: Challenge 2
    • 3.5: Solution 2
  • Part 4: JSX Challenge
    • 4.1: JSX Overview
    • 4.1: Challenge Answer
    • 4.2: className
  • Part 5: Class Concepts
    • 5.1: State
    • 5.2: setState
    • 5.3: Class Components Challenge
    • 5.4: Class Components Challenge Answer
  • Part 6: Props Starter
    • 6.0: Props Overview
    • 6.1: Props Demo and Challenge 1
    • 6.2: Props Answer 2
    • 6.3: Props Passing Functions and Challenge 2
    • 6.4: Props Answer 2
    • 6.5: External Props and Mapping Components
    • 6.6: PropTypes
  • Part 7: Lifecycle Methods
    • 7.0: Lifecycle Overview
    • 7.1: Lifecycle Methods
    • 7.2: Mounting Methods
    • 7.3: Update Methods
    • 7.4: Unmount Methods
  • Part 8: Apps
    • 1.0 - Small Timer Apps
      • 1.1 - Simple Timer
      • 1.2 - Clock App
      • 1.3 - Stop Watch App
      • 1.4 - Challenge
    • 2.0 - Concept List
      • 2.1 - Concept Data
      • 2.2 - Concept App Component
      • 2.3 - Concept Component
      • 2.4 - Concept List Component
    • 3.0 - NYT
      • 3.1 - NytApp Component
      • 3.2 - Nyt Results
    • 4.0 - The Friend List App
      • 4.1 - Friend List App Component
      • 4.2 - Friend Component
    • 5.0 - Movie Search Application
      • 5.1 - Form Component
      • 5.2 - Form Results Component
      • 5.3 - Styled Components
    • 6.0 - YouTube Api
      • 6.1 - Video Component
      • 6.2 - Video Component
      • 6.3 - Video Component
    • 7.0 - Github Api Application
      • 7.1 - The Users
      • 7.2 - Github API Component
      • 7.3 - Github API Card
      • 7.4 - Github API Card Form
      • 7.5 - Github API Card List
      • 7.6 - Github API Search
      • 7.7 - Github API Challenge
    • 8.0 - Bitcoin Api Application
      • 8.1 - Bitcoin API Setup
      • 8.2 - Bitcoin API Fetch
      • 8.3 - Bitcoin API Line Chart
      • 8.4 - Bitcoin API Fetching Data
      • 8.5 - Bitcoin API Info Box
      • 8.6 - Bitcoin API Completed Code
    • 9.0 - Google Maps Api Challenge
    • 10.0 - Sound Cloud App Challenge
    • 11.0 - VR App Challenge
    • 12.0 - React Native Intro
  • Part 9: Project
  • Part 10: Notes
    • 10.1 - Resources
    • 10.2 - Troubleshooting
Powered by GitBook
On this page
  • Class Components with Props
  • Functional Component Using Props
  1. Part 6: Props Starter

6.1: Props Demo and Challenge 1

Let's begin to understand props by creating a Class Component that will pass props to a Functional Component. We'll stick with these two components as we move forward throughout this module. They should provide you the tools to gain a cursory understanding of props in React.

Class Components with Props

To learn props, it's best to have a working example to play around with. Let's build one out:

In the components/concepts folder create a new file called PropsDemo.js.

Let's import react in the file.

import React from 'react';

Let's set up a regular class component called PropsDemo.js:

export default class PropsDemo extends React.Component {
    render() {
        return (
            <div>
                <FunctionalComp string="will this display?"/>
            </div>
        )
    }
}

This class component is passing a prop of 'string' to our functional component, which still needs to be built out. Let's build our functional component out below. It will be contained in the same file as PropsDemo, just below the PropsDemo component.

Functional Component Using Props

const FunctionalComp = (props) => {
    return(
        <div>
            <p>{props.string}</p>
        </div>
    )
}

If you run your project, can you see output in Chrome? If not, great! That's to be expected. We still need to add a link from our Sidebar component and route from that link. Let's start by importing PropsDemo into Sidebar.js.

import PropsDemo from '../concepts/PropsDemo';

Let's add the following Link in our Sidebar.js file:

<li><Link to="/propsdemo">Props Demo</Link></li>

Finally, let's add a route that will be triggered upon the propsdemo link being clicked:

<Route exact path="/propsdemo"><PropsDemo/></Route>

Let's consider what has been built out here. In the class component, you have called a functional component with the prop 'string'. This class is not responsible for displaying to the DOM. It only calls a child component (the functional component) which handles all of the DOM display. The functional component itself doesn't actually display a specific value. The information contained inside the <p> tags is determined by the props object passed to the functional component. {props.string} shows whatever the value of string is from the prop.

Your challenge to be completed before you move to the next page is to make 3 more component calls to FunctionalComp inside of PropsDemo (so you'll have 4 total). For each of these component calls, pass a prop of string that will display a string of your choosing. A sample solution will be shared on the next page.

Previous6.0: Props OverviewNext6.2: Props Answer 2

Last updated 6 years ago

Now when you click on the propsdemo link from the sidebar you should be able to view the following:

Props Start