Giter Club home page Giter Club logo

bill-splitter-react's Introduction

Bill Splitter (React example)

What's this?

  • React勉強のために作ったサンプル。
  • create-react-app で作成し、TypeScriptでコードを書いた。

How to execute?

$ yarn install
$ yarn start

Memo

TypeScriptでReactアプリを書く場合のメモ書き。

プロジェクトの作成

npm 5.2以降は入ってるとして、以下でプロジェクトを作成。

$ npx create-react-app bill-splitter-react --typescript

ステートフルコンポーネントの作成

  • TypeScriptなので、プロパティとステートはちゃんと型を定義して。
import React, { Component } from 'react';

type DetailProps = {
  name: string;
  kind: string;
  amount: number;
  deletable: boolean;
  rowIndex: number;
  onNameChange: (rowIndex: number, name: string) => void;
  onKindChange: (rowIndex: number, kind: string) => void;
  onAmountChange: (rowIndex: number, amount: number) => void;
  onDeleteClick: (rowIndex: number) => void;
  onAddClick: (rowIndex: number) => void;
}
  • Component 継承時に、プロパティとステートの型パラメータを指定する。
  • render()JSX を返す。
  • TypeScriptの場合、 classclassName と書かないと怒られる。同様に <label>forhtmlFor で。
// 明細コンポーネント
class Detail extends Component<DetailProps, {}> {
  render() {
    return (
      <div className="bs-detail">
        <input
          type="text"
          value={this.props.name}
          placeholder="名前を入力"
          onChange={e => this.handleNameChange(e)}
        />
  • イベントの型は any でもいいんだけど、ちゃんと書くとこんな感じ
    handlePaymentChange(e: React.ChangeEvent<HTMLInputElement>) {
        const payment = Number(e.target.value);
        if (payment) {
            this.props.onChange(payment);
        }
    }

bill-splitter-react's People

Contributors

yonetty avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.