0
1

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 3 years have passed since last update.

React入門

Posted at

こちらの記事は以下の書籍を参考にアウトプットとして執筆しました
React入門 React・Reduxの導入からサーバサイドレンダリングによるUXの向上まで

Reactコンポーネント

  • Functional Component(関数のような定義)
  • Class Component(クラスのような定義)

違いはClass Componentでは以下が使える。

  • state
  • ライフサイクルメソッド

コンポーネント再利用とReactエレメント

ReactDOM.render(
    <div>
        <Hello />//いくつでも書ける
        <Hello />//これが再利用するということ
        <Hello />//また、ここまでのやつはReactエレメント
    </div>,
    document.getElementById('root')
)

Reactエレメントは変数へ格納可能

let g=<Hello />

ReactDOM.render(
    <div>
      {g}
    </div>,
    document.getElementById('root')
)

children

特殊なprops
Reactコンポーネントノコ要素がchildrenとして渡される

import React from 'react';
import ReactDOM from 'react-dom';



const Hello = (props) => {
  return (
    <div>こんちわ{props.children}さん</div>
  )
}

ReactDOM.render(
  <div>
    <Hello>
        A
    </Hello>
  </div>,
  document.getElementById("root")
)

参考:React入門 React・Reduxの導入からサーバサイドレンダリングによるUXの向上まで

import React from 'react';
import ReactDOM from 'react-dom';



const Hello = (props) => {
  return (
    <div>こんちわ{props.children}さん</div>
  )
}

const Greeting = (props) => {
  return (
    <div>
      <div>ご挨拶</div>
      {props.children}
    </div>
  )
}

ReactDOM.render(
  <div>
    <Greeting>
      <Hello>A</Hello>
      <Hello>B</Hello>
    </Greeting>
  </div>,
  document.getElementById("root")
)

上記は複雑なケース
Greetingのprops.childrenにはHelloエレメントが渡される。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?