Skip to content

Instantly share code, notes, and snippets.

@djD-REK
Created August 19, 2019 23:14
Show Gist options
  • Select an option

  • Save djD-REK/3682f4572ac219a8a0539a014e189b8c to your computer and use it in GitHub Desktop.

Select an option

Save djD-REK/3682f4572ac219a8a0539a014e189b8c to your computer and use it in GitHub Desktop.
// Before React Hooks: Imperative Programming Paradigm
// This is an ES6 class extending from React.Component, with an internal state:
import React, { Component } from "react";
export default class Button extends Component {
constructor() {
super(); // Prototypal inheritance is explicit here
this.state = { buttonText: "Click me, please" };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
// We directly mutate the state and specify exactly how to do so (imperative paradigm)
this.setState(() => {
return { buttonText: "Thanks, been clicked!" };
});
}
render() {
const { buttonText } = this.state;
return <button onClick={this.handleClick}>{buttonText}</button>;
}
}
// Refactored using React Hooks: Declarative Programming Paradigm
import React, { useState } from "react";
export default function Button() {
const [buttonText, setButtonText] = useState("Click me, please");
function handleClick() {
// We declare the state should change but don't specify how to do it (functional paradigm)
return setButtonText("Thanks, been clicked!");
}
return <button onClick={handleClick}>{buttonText}</button>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment