LoginSignup
6
5

More than 3 years have passed since last update.

[React]superのpropsとは何か。

Posted at

super (props)のprops とは何ぞや

リアクトのコンポーネントを作成する時、いつも

class Hello extends React.Component {
    constructor (props) {
        super(props)
        // イベントハンドラをthisで結びつける
        this.clickHandler = this.clickHandler.bind(this)
    }
}
super (props)

を書いている。
これは何を意味しているのか。

propsを


console.log (props)

すると、


ReactDOM.render(
    <Hello name="クジラ"/>,
    document.getElementById('root')
)

ここで渡されていた
name="クジラ" が表示された。

propsの結果はコンポーネント呼び出しのとき渡された値だと判明

なぜpropsの値をsuperするのか

参考 : https://qiita.com/hand-dot/items/61a4b808f110b12e4281

結論 :

super(props)を定義してからでないと
this.state等でprops内の値を新規追加出来ないから。

6
5
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
6
5