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

【React】Presentational Component と Container Component

Posted at

Component

  • ReactはUIを Component という部品単位で扱う
  • Presentational ComponentContainer Component に分けることで、見た目とロジックを切り分けることができる

Presentational Component

  • 表示のみに専念する
  • 自分のコンポーネント以外のことについて依存しない
  • 親からpropsとしてデータやコールバックを受け取る
components/App.js
import React from 'react';

const App = ({ text: string, onButtonClick: () => void }) => (
  <div>
    <p>{text}</p>
    <button onClick={onButtonClick}>Click</button>
  </div>
);

export default App;

Container Component

  • ロジックのみに専念する
  • Presentational Componentに具体的なデータやコールバックを渡す
containers/App.js
import App from "../components/App";
import { connect } from "react-redux";

const mapStateToProps = state => {
  return {
    text: state.text
  };
};

const mapDispatchToProps = dispatch => {
  return {
    onButtonClick: () => {
      dispatch({ type: "CLICK" });
    }
  };
};

export default connect(mapStateToProps, mapDispatchToProps)(App);

Component の分離によるメリット

  • アプリケーション部分とUI部分を分離できる
  • 表示するデータやボタン押下時の処理を外部から指定することができる
    -> 再利用性が上がる
3
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
3
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?