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
. Iftrue
, 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:
- The image disappears on click when
hasImage
is toggled tofalse
. - 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!