02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Components that change with state

Understanding Components that Change with State

In this lecture, we explored the concept of state in React, diving into how it enables components to handle dynamic behavior. State complements props by allowing components to track and react to changes internally.


Key Takeaways

1. State vs. Props

  • Props: Data passed into a component, generally immutable from the component's perspective.
  • State: Managed within the component and expected to change over time.

Implementing State

We modified the PortfolioItem component to introduce state. The process included:

a. Adding a Constructor

The constructor was defined using ES6 class syntax:

constructor(props) {
    super(props);
    this.state = {
        hasImage: true, // Initial state
    };
}
  • super(props): Ensures the parent class (React.Component) receives the props.
  • State initialization: Defines the initial value for hasImage.

b. Using State in the Render Function

To conditionally render the component based on the state:

{this.state.hasImage ? (
    <img src={this.props.imgPath} alt="Portfolio Item" />
) : null}
  • Ternary Operator: Conditionally checks hasImage. If true, the image is displayed; otherwise, null is returned.

c. Handling Events to Update State

An onClick handler was added to toggle the visibility of the image:

onUpdateImageState = () => {
    this.setState({ hasImage: !this.state.hasImage });
};
  • setState: Updates the state and triggers a re-render.
  • Bound Methods: Used ES6 arrow functions to ensure this remains scoped to the component.

Example of the Updated Component

Here’s the full updated code for the PortfolioItem:

import React from 'react';

class PortfolioItem extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            hasImage: true,
        };
        this.onUpdateImageState = this.onUpdateImageState.bind(this);
    }

    onUpdateImageState() {
        this.setState({ hasImage: !this.state.hasImage });
    }

    render() {
        return (
            <div onClick={this.onUpdateImageState}>
                {this.state.hasImage ? (
                    <img src={this.props.imgPath} alt="Portfolio Item" />
                ) : null}
            </div>
        );
    }
}

export default PortfolioItem;

Highlights of ES6 Features

  • Class Constructors: Simplified the instantiation of components and passing props to parent classes.
  • Arrow Functions: Ensured proper scoping of this in methods.
  • Conditional Rendering: Used the ternary operator for concise logic.

Testing State Changes

The application was tested to ensure that:

  1. The image disappears on click when hasImage is toggled to false.
  2. The image reappears when toggled back to true.

Recap

This lecture emphasized the difference between props and state, showcasing how state allows components to handle internal changes. By integrating dynamic behavior, React components become more interactive and responsive.

In the next lecture, we'll expand on these concepts, introducing arrow functions and further exploring ES6 features to streamline React development.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!

Using Components inside of Components

Learn how to build reusable React components by nesting components inside other components. This lecture covers componentization, reusability, and dynamic components.

07:12

Passing Properties to Components

Learn how to use props in React to create dynamic, reusable components. This lecture also explains the difference between props and state.

05:59

Dynamically Passing Components into Components

Learn to pass components dynamically into other components in React, utilizing props.children, ES6 destructuring, and imports/exports to modularize and enhance code reusability.

09:14

Components that change with state

Learn about React state and its role in creating dynamic, interactive components. Understand the distinction between props and state, and explore ES6 features for better React development.

10:08