0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Reactのモデルのメモ

0
Posted at

モデルでの配列の渡し方

eg)familyの中にchildが複数人存在する時

models/family.js
this.children = (children || []).map(data => Child.newChild(data))

渡したいものが一つではないとき、配列で受け取ったパラメータにmap処理でArray(Childモデルのインスタンスの配列という意味)が渡るようにする。

継承

accountモデルを継承したchildモデル

export default class Account {
  constructor({
    id,
    nickname,
    email,
  }) {
    this.id = id
    this.nickname = nickname
    this.email = email
  }

  static newFromApiResponse = data => {
    return new Account(transformSnakeToCamel(data))
  }
}

// ここからchildモデル
export class Child extends Account {
  constructor(params) {
    super(params)
    this.type = "child"
  }

  static newFromApiResponse = data => {
    return new Child(transformSnakeToCamel(data))
  }
}

paramsを引数としてsuperでaccountモデルにあるデータを継承する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?