Skip to main content

8.38 Improving Perfomance

Converting from functional to class and editing

Modal.js

Modal.js
import React, { Component, Fragment } from "react";
import Backdrop from "../Backdrop/Backdrop";
import classes from "./Modal.module.css";

class Modal extends Component {
shouldComponentUpdate(nextProps, nextState) {
return nextProps.show !== this.props.show;
}

componentWillUpdate() {
console.log("[Modal] WillUpdate");
}
render() {
return (
<Fragment>
<Backdrop show={this.props.show} clicked={this.props.modalClosed} />
<div
className={classes.Modal}
style={{
transform: this.props.show ? "translateY(0)" : "translateY(-100vh)",
opacity: this.props.show ? "1" : "0",
}}
>
{this.props.children}
</div>
</Fragment>
);
}
}

export default Modal;

OrderSummary.js

OrderSummary.js
import React, { Component, Fragment } from "react";
import Button from "../../UI/Button/Button";

class OrderSummary extends Component {
// This could be a functional component, doesn't have to be a class
componentWillUpdate() {
console.log("[OrderSummary] WillUpdate");
}
render() {
const ingredientSummary = Object.keys(this.props.ingredients).map(
(igKey) => {
return (
<li key={igKey}>
<span style={{ textTransform: "capitalize" }}>{igKey}</span>:{" "}
{this.props.ingredients[igKey]}
</li>
);
}
);

return (
<Fragment>
<h3>Your Order</h3>
<p>A delicious burger with the following ingredients:</p>
<ul>{ingredientSummary}</ul>
<p>
<strong>Total Price: {this.props.price.toFixed(2)}</strong>
</p>
<p>Continue to Checkout?</p>
<Button btnType="Danger" clicked={this.props.purchaseCancelled}>
CANCEL
</Button>
<Button btnType="Success" clicked={this.props.purchaseContinued}>
CONTINUE
</Button>
</Fragment>
);
}
}

export default OrderSummary;