2016-10-26 39 views
1

を反応させるのに最高の状態/設定された第1の状態を初期化する方法: 今、私は私の減速で、次のやっているために/ Reduxの

export default function (state = null, action) { 

    if(state==null) 
    { 
     state = [ 
      { 
       id: 1, 
       first: "Bucky", 
       last: "Roberts", 
       age: 71, 
       description: "Bucky is a React developer and YouTuber", 
       thumbnail: "http://i.imgur.com/7yUvePI.jpg" 
      }, 
      { 
       id: 2, 
       first: "Joby", 
       last: "Wasilenko", 
       age: 27, 
       description: "Joby loves the Packers, cheese, and turtles.", 
       thumbnail: "http://i.imgur.com/52xRlm8.png" 
      }, 
      { 
       id: 3, 
       first: "Madison", 
       last: "Williams", 
       age: 24, 
       description: "Madi likes her dog but it is really annoying.", 
       thumbnail: "http://i.imgur.com/4EMtxHB.png" 
      } 
     ] 
    } 

    switch (action.type) { 
     case 'USER_DELETED': 
      return state.filter(user => user.id !== action.userIdToDelete); 
    } 

    return state; 
} 

は、だから私は、状態がnullであるかどうかをチェックし、それがある場合、私はそれを取り込みます。これを行うためのより良い方法がありますか?つまり、内部から減速機を投入しないのですか?代わりにヌルへのデフォルト値を持つの

答えて

1

...私には悪い習慣のように見える - あなたは常にあなたはそれが期待するタイプにデフォルト状態の値を設定する必要があり、次の

 initialState = [ 
     { 
      id: 1, 
      first: "Bucky", 
      last: "Roberts", 
      age: 71, 
      description: "Bucky is a React developer and YouTuber", 
      thumbnail: "http://i.imgur.com/7yUvePI.jpg" 
     }, 
     { 
      id: 2, 
      first: "Joby", 
      last: "Wasilenko", 
      age: 27, 
      description: "Joby loves the Packers, cheese, and turtles.", 
      thumbnail: "http://i.imgur.com/52xRlm8.png" 
     }, 
     { 
      id: 3, 
      first: "Madison", 
      last: "Williams", 
      age: 24, 
      description: "Madi likes her dog but it is really annoying.", 
      thumbnail: "http://i.imgur.com/4EMtxHB.png" 
     } 
    ]; 

    export default function (state = initalState, action) { 
    ... 
    } 
1

を行いますそう、ここでfunction (state=[])が良いです。それをヌルに設定することは反パターンです。 http://redux.js.org/docs/api/createStore.html

+0

感謝:

は、デフォルトの初期状態などのデータ用として使用するには、初期状態を設定するCREATESTOREで二番目の引数を使用する必要があります。しかし、私の2番目の引数は 'applyMiddleware(サンク、約束、ロガー)'これは間違っている? –

+1

初期(プリロード済み)状態を含めると、適用するミドルウェアが3番目の引数になります。 したがって、私はcreateStore(reducer、[preloadedState]、[enhancer])を送信したリンクで、エンハンサーはミドルウェアです。 – joejknowles

関連する問題