Skip to content

Instantly share code, notes, and snippets.

@andrit
Created August 10, 2018 20:40
Show Gist options
  • Save andrit/451db8b43a132c92767b338005db3ce5 to your computer and use it in GitHub Desktop.
Save andrit/451db8b43a132c92767b338005db3ce5 to your computer and use it in GitHub Desktop.
const withAmount = currencyComponents =>
  class Amount extends Component {
    constructor(props) {
      super(props);

      this.state = {
        amount: 0,
      };
    }

    onIncrement = () => {
      this.setState(state => ({ amount: state.amount + 1 }));
    };

    onDecrement = () => {
      this.setState(state => ({ amount: state.amount - 1 }));
    };

    render() {
      return (
        <div>
          <span>US Dollar: {this.state.amount} </span>

          <button type="button" onClick={this.onIncrement}>
            +
          </button>
          <button type="button" onClick={this.onDecrement}>
            -
          </button>

          {currencyComponents.map(CurrencyComponent => (
            <CurrencyComponent amount={this.state.amount} />
          ))}
        </div>
      );
    }
  };
const Euro = ({ amount }) => <p>Euro: {amount * 0.86}</p>;

const Pound = ({ amount }) => <p>Pound: {amount * 0.76}</p>;

const CurrenciesWithAmount = withAmount([Euro, Pound]);
const App = () => <CurrenciesWithAmount />;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment