2016-09-08 4 views
1

Ich erstelle eine React Native App. Mein index.ios.js wo ich Middlewares zum Laden hinzufüge.Was ist los mit meiner Redux-Saga

import React, { Component } from 'react' 
import { 
    AppRegistry, 
} from 'react-native' 
import { Provider } from 'react-redux' 
import { applyMiddleware, createStore } from 'redux' 
import createLogger from 'redux-logger' 
import createSagaMiddleware from 'redux-saga' 

import reducer from './src/reducers' 
import * as sagas from './src/sagas' 
import Scene from './src/components/scene' 

const store = createStore(
    reducer, 
    applyMiddleware(createSagaMiddleware(...sagas), createLogger()) 
); 

const CitiesApp =() => (
    <Provider store={store}> 
    <Scene /> 
    </Provider> 
); 

AppRegistry.registerComponent('CitiesApp',() => CitiesApp); 

Ich habe eine Aktion:

export const USER_FETCH_REQUESTED = 'USER_FETCH_REQUESTED'; 
export const fetchUser =() => ({ 
    type: USER_FETCH_REQUESTED 
}); 

Was ich von componentDidMount in einer Komponente nennen

componentDidMount() { 
    this.props.dispatch(fetchUser()); 
    console.log('componentDidMount') 
} 

Auch habe ich eine Saga:

export function* watchUserFetch() { 
    console.log('watchUserFetch'); 
    yield take(actions.USER_FETCH_REQUESTED); 
    yield fork(fetchUser); 
} 

function* fetchUser() { 
    try { 
    const user = yield call(() => Api.fetchUser()); 
    yield put({type: actions.USER_FETCH_SUCCEEDED, user}); 
    } catch (e) { 
    yield put({type: actions.USER_FETCH_FAILED, message: e.message}); 
    } 
} 

Aber die Saga funktioniert nicht. Ich sah nicht watchUserFetch in der Konsole und fetchUser Funktion läuft nicht.

Wo ist mein Fehler? Warum läuft fetchUser nicht?

Antwort

1
  1. Wurzel Saga erstellen
    export default function* rootSaga() { 
        yield [ 
         watchUserFetch() 
        ] 
    } 
    
  2. dann rootSaga laufen

    import createSagaMiddleware from 'redux-saga'; 
    import rootSaga from './sagas.js'; 
    
    const sagaMiddle= createSagaMiddleware(); 
    
    const store = createStore(reducer, 
        applyMiddleware(sagaMiddle,createLogger()) 
    ); 
    
    sagaMiddle.run(rootSaga); 
    

webpackbin example

+0

vielen Dank! – rel1x