LoginSignup
1
2

More than 5 years have passed since last update.

redux Loop コンポーネントで幸せになりたい!!

Last updated at Posted at 2017-02-14

redux Loop コンポーネントで幸せになりたい!!

目的

  • redux 汎用 Loop コンポーネント
  • mapを使ってapiから受け取ったデータを表示していたけど、 ループなんだからやってることは一緒なんじゃね?
Loop
const api = [
 {
   id: "123",
   text:" hoge"
 }
]

<Loop data={api}>
  <Item>
</Loop>

みたいな感じで書きたい。。。

やってみた!!

Loopコンポーネント

初期の実装

まぁ、とりあえずchildrenを表示させるけど
this.props.childrenにどうやって新しくprops渡せばいいんだ????

Loop

const Loop = ({children}) => {
  return (
      <ul>
         {children}
      </ul>
  );
};

改良版 React.cloneElementでpropsを新しく追加

React.cloneElementでpropsを新しく追加

Loop

const Loop = ({data,children}) => {
  return (
      <ul>
         {
          data.map((item,index)=> {
            return (
              React.cloneElement(
                children,{
                 item,
                 key:index
                }
              )
            ) 
          })
        }
      </ul>
  );
};

参考にさせていただいたサイト

あとはItem コンポーネント側で this.props.itemから取りたい値を取ってくるだけ!
React + redux は奥が深い・・・

多分、いろいろと不備があると思うので(keyとか)アドバイス等あればお願いします!!

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