2017-08-17 12 views
0

私はこれらのファイルを持っていて、何かをディスパッチすると、常に減速機のデフォルトのケースを返します。React-Redux |ディスパッチャーが動作しない

これは私がReduxの/サンクを使用している、と私はこのチュートリアル以下のよ初めてです:https://www.youtube.com/watch?v=nrg7zhgJd4wをし、彼はそれをやっているとき、それは動作します。..

私のコードを見てください。

import React, { Component } from 'react'; 
import './App.css'; 
import Request from 'request'; 
import { connect } from 'react-redux' 

import * as OperationsActions from './actions/operationsReducerActions' 

//import { Content, FilterSelect, ListItem, Searchbar, Sidebar} from './components/index.js' 

function mapStateToProps(state){ 
    return { 
    operations : state.operations 
    } 
} 

class App extends Component { 
    constructor(props){ 
    super(props); 
    } 
    componentDidMount(){ 
    this.props.dispatch(OperationsActions.getOperations()); 
    } 
    render() { 
    console.log(this.props) 
    return(
     <div>{this.props.operations.operations[0].text}</div> 
    ) 
    } 
} 

export default connect(mapStateToProps)(App) 

アクションファイル:

import Request from 'request'; 

export function getOperations(){ 
     Request('http://localhost:8000/client/REQUEST_OPERATIONS', (error, response, data) => { 
      if(!error){ 
       return {type : 'FETCH_OPERATIONS_SUCCES', payload : data}; 
      } 
      else { 
       return {type : 'FETCH_OPERATIONS_REJECTED', payload : error} 
      } 
    }); 
} 

は、コンポーネントの反応します減速:

export default function reducer(state={ 
     operations : [{ 
      text : '', 
      reqArgument : '', 
      filters : [], 
      url : '' 
     }], 
     fetching : false, 
     fetched : false, 
     error : null 
    }, action) { 

     switch(action.type){ 
      case 'FETCH_OPERATIONS':{ 
       return {...state, fetching : true } 
      } 
      case 'FETCH_OPERATIONS_REJECTED':{ 
       return {...state, fetching : false, error : action.payload} 
      } 
      case 'FETCH_OPERATIONS_SUCCES':{ 
       return {...state, fetched : true, fetching : false, operations : action.payload } 
      } 
      default : { 
       return {...state} 
      } 
     } 
    } 

と私の店:

'Reduxの'

import logger from 'redux-logger' 
    import thunk from 'redux-thunk' 
    import promise from 'redux-promise-middleware' 

    import reducer from './reducers' 

    const middleware = applyMiddleware(promise, thunk, logger) 

    export default createStore(reducer, middleware) 
+0

?できれば試してみてください。画面に何も表示されませんか?あなたは 'console.log()'で任意の値を記録しようとしましたか? –

+0

はい私はコンソールのログを記録しようとしましたが、問題は私のレデューサーが常にデフォルトのケース/状態を戻し続けているか、ディスパッチャーが正常に動作していないことです... –

答えて

1

からの輸入{applyMiddleware、CREATESTORE}問題

class App extends Component { 
    ... 
    componentDidMount(){ 
    this.props.dispatch(OperationsActions.getOperations()); 
    } 
    ... 
} 

OperationsActions.getOperations()undefinedを返します呼び出す - それはdoesnの明示的に何も返さない。結果として、減速機はaction引数としてundefinedを受信します。 switchはデフォルトのケースを実行します。

ソリューション

あなたgetOperationsアクションは、サンクに変換する必要があります。ここでの例である:

function getOperationsThunk() { 
    return function (dispatch) { 
    Request('http://foo.bar', (err, data) => { 
     if (err) { dispatch(errorAction(err)); } 
     else { dispatch(successAction(data)); } 
    }); 
    }; 
} 

errorActionsuccessAction機能は、非同期要求からデータを受信し、ディスパッチするアクション・オブジェクトを作成します。

さらに読み、正確に動作していない何

+0

はい、私は試しましたが、サンクの内部では、動作しませんでした。今度は、オブジェクトを返す関数として定義された別個のアクションを追加しました。これらのアクションはディスパッチして動作します: –

+0

@LaurensMäkel:アクション作成者関数の代わりにオブジェクトリテラルを使用すると違いはありません。あなたはその試みを含めるためにあなたの質問を拡大することができ、私はそれを見直します。しかし、とにかくアクションクリエーターを持つことは良い習慣です([here](http://blog.isquaredsoftware.com/2016/10/idiomatic-redux-whyuseuse-action-creators/)または[公式文書](http ://redux.js.org/docs/basics/Actions.html#action-creators)理由:tldr:モジュール、DRY、テスト可能)。うれしい今それは働いている! – wing