LoginSignup
0
0

More than 3 years have passed since last update.

StateとPropsの違い(React)

Posted at

ReactのStateとPropsの違いがよくわからなかったのでメモ

State

:point_up_tone1:変更可能
:point_up_tone1:そのコンポーネント自体が値を持っている
:point_up_tone1:setStateで更新することができる

*Propsだけでできなかった時に使うようにする(なるべく使わない)

Props

:star:外部からもらう値
:star:Readオンリー(変更不可)→通信が早い

「mapStateToProps」と「mapDispatchToProps」

:airplane:mapStateToProps
・stateの値をcompornentのpropsに入れる

:airplane:mapDispatchToProps
・グローバルステート:React-reduxのstate
・React コンポーネントのツリーの一番上流(Buttonとか)

:helmet_with_cross::helmet_with_cross::helmet_with_cross:connect:helmet_with_cross::helmet_with_cross::helmet_with_cross:
connectを使うことで、親→子ではなく親→孫、ひ孫...にも渡すことができるようになる

const mapStateToProps = ({ app, taskSubscription }) => ({
  isLoading: app.isLoading,
  taskSubscription: taskSubscription.selected
})

const mapDispatchToProps = dispatch => ({
 //ページタイトルはこのサービス内全てで共通
  setPageTitle: pageTitle => dispatch(setPageTitle(pageTitle)),
})

export default connect(  //ここで渡す
  mapStateToProps,
  mapDispatchToProps)

:writing_hand_tone1:(メモ)exportとは?
定義したものを外部からインポートできるようにする
defaultありとなしではimportの仕方が異なる。
:exclamation:defaultは1つのファイルに一つしか定義できない

export default AAA
import AAA from './sample.js'

export BBB
import { BBB } from './sample.ls'
*{}をつける

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