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 Component - Simple
  • Component Set up
  • Component constructor
  • this.state
  • render()
  • Calling the component
  • Calling Components inside of Components
  • Reflection
  1. Part 5: Class Concepts

5.1: State

Class Component - Simple

In the components folder, let's create a new React Component file called ClassComponentDemo.js.

A react class component starts with an import, so let's import it:

import React, { Component } from 'react';

Component Set up

Next we'll set up a class component and export it so that it is accessible elsewhere, namely where we'll call it in the Sidebar.js file:

export default class ClassComponentDemo extends Component {


}

Notice already that this component is an ES6 class that uses the extends keyword. It's inheriting from Component which is part of React. Here we see a very practical use of the concepts learned in the last module.

Component constructor

Next, we'll add a constructor. The props parameter in the constructor is also inherited from React. We'll talk about that at great length in a future module. So let's hold off on that for now. Since we are inheriting from React, we call super so that we can use the this keyword in the constructor.

export default class ClassComponentDemo extends Component {
    constructor(props) {
        super(props);
    }
}

this.state

Let's use the this keyword. Here we'll call this.state. We have yet to talk about state, but we will in the next module. Know for now, that we are creating the initial state of our application.

export default class ClassComponentDemo extends Component {
    constructor(props) {
        super(props);
        this.state = { simpleMessage: "Welcome user!!!" };
    }
}

render()

The constructor is not required here, but in all class components in React, a render method is required. This is the JSX that gets packaged up, exported, and rendered when our component gets called.

export default class ClassComponentDemo extends Component {
    constructor(props) {
        super(props);
        this.state = { simpleMessage: "Welcome user!!!" };
    }

    render() {
        return (
            <h1>Hello</h1>
        );
    }
}

Calling the component

Review question: If we run the app right now, will anything happen?

The answer would be 'no'. Why?

The reason is that we still have to call the component.

Let's do that real quick.

  1. In Sidebar.js add an import, the link and the route.

  2. Feel free to run the app. You should see the following:

At this point we're doing nothing with the state that we initialize in our constructor. Below please see how we will use that state within the application.

export default class ClassComponentDemo extends Component {
    constructor(props) {
        super(props);
        this.state = { simpleMessage: "Welcome user!!!" };
    }

    render() {
        return (
            <div className="main">
                <div className="mainDiv">
                    <h4>
                        {this.state.simpleMessage}
                    </h4>
                </div>
            </div>
        );
    }
}

Again, state infers something that shows a possibility of transmutation and that has a possibility to change based on some other factor. You don’t use state with an application that is just showing static content. As the docs themselves say, 'State is reserved only for interactivity, that is, data that changes over time'.

Here's a simple example of state that you might see everyday in class. You're on Slack, and suddenly you see that 'Paul O'Connor is typing'. They are typing a message to you and the state has changed to show that message. Then, they send the message, and the DOM reacts to that event by revealing the new message in your conversation feed. The state has changed, and a new state has been created, and the DOM is listening and waiting to hear another event. State is omnipresent in our software. What other examples can you think of?

Calling Components inside of Components

It's important to note and remind you here that we can create another component(Class or Functional) and call it inside of the ClassComponentDemo. See the ClassComponentNotes at the bottom. Also, notice how we're calling it in the return of the render method in ClassComponenDemo:

export default class ClassComponentDemo extends Component {
    constructor(props) {
        super(props);
        this.state = { simpleMessage: "Welcome user!!!" };
    }

    render() {
        return (
            <div className="main">
                <div className="mainDiv">
                    <ClassComponentNotes />
                    <h4>
                        {this.state.simpleMessage}
                    </h4>
                </div>
            </div>
        );
    }
}

const ClassComponentNotes = function () {
    return (
        <div>
            <h1>Class Components</h1>

            <p>Class components are considered the "React way" of writing components.</p>
            <dl>
                <dt>ES6 JS Classes</dt>
                <dd>Built on these, must understand them</dd>
                <dt>must extend Component </dt>
                <dd>Class components need to extend the React Component.</dd>
                <dt>render()</dt>
                <dd>Class components must always have a render method.</dd>
                <dt>export</dt>
                <dd>Only one class component exported per file.</dd>
            </dl>
        </div>
    );
};

Such structure is quite common and allows us to easily separate concerns and work with components in a highly modularized way. To remove the ClassComponentNotes from the app, all we have to do now is remove the call. Go ahead and add the above code into your app.

Reflection

Give yourself to absorb the concept of state. This concept is a cornerstone of React. The name itself comes from this concept in state. React is designed to listen for events and 'react' to changes in the state of an application.

PreviousPart 5: Class ConceptsNext5.2: setState

Last updated 7 years ago

Consider this : How many ways is state changing?

React application
Welcome