我的 redux-saga 有什么问题
What's wrong with my redux-saga
我创建了一个 React Native 应用程序。我的 index.ios.js
我将中间件添加到商店的地方。
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);
我有一个动作:
export const USER_FETCH_REQUESTED = 'USER_FETCH_REQUESTED';
export const fetchUser = () => ({
type: USER_FETCH_REQUESTED
});
我在组件componentDidMount
中调用的
componentDidMount() {
this.props.dispatch(fetchUser());
console.log('componentDidMount')
}
我也有一个传奇:
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});
}
}
但是这个故事行不通。我没有在控制台中看到 watchUserFetch
,fetchUser
函数也没有 运行。
我的错误在哪里?为什么 fetchUser
不 运行?
创建根传奇
export default function* rootSaga() {
yield [
watchUserFetch()
]
}
然后 运行 rootSaga
import createSagaMiddleware from 'redux-saga';
import rootSaga from './sagas.js';
const sagaMiddle= createSagaMiddleware();
const store = createStore(reducer,
applyMiddleware(sagaMiddle,createLogger())
);
sagaMiddle.run(rootSaga);
我创建了一个 React Native 应用程序。我的 index.ios.js
我将中间件添加到商店的地方。
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);
我有一个动作:
export const USER_FETCH_REQUESTED = 'USER_FETCH_REQUESTED';
export const fetchUser = () => ({
type: USER_FETCH_REQUESTED
});
我在组件componentDidMount
中调用的
componentDidMount() {
this.props.dispatch(fetchUser());
console.log('componentDidMount')
}
我也有一个传奇:
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});
}
}
但是这个故事行不通。我没有在控制台中看到 watchUserFetch
,fetchUser
函数也没有 运行。
我的错误在哪里?为什么 fetchUser
不 运行?
创建根传奇
export default function* rootSaga() { yield [ watchUserFetch() ] }
然后 运行 rootSaga
import createSagaMiddleware from 'redux-saga'; import rootSaga from './sagas.js'; const sagaMiddle= createSagaMiddleware(); const store = createStore(reducer, applyMiddleware(sagaMiddle,createLogger()) ); sagaMiddle.run(rootSaga);