redux-saga官方demo的一个问题请教!

我的sgags代码:

import { put, call, take,fork } from 'redux-saga/effects';

export const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

function* incrementAsync() {
    yield call(delay, 1000);
    yield put({ type: 'INCREMENT' });
}
export default function* watchIncrementAsync() {
    while(true){
        yield take('INCREMENT_ASYNC');
        yield fork(incrementAsync);
    }
}

在main页面使用sagas:

import "babel-polyfill"

import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, applyMiddleware } from 'redux'
import createSagaMiddleware from 'redux-saga'

import Counter from './Counter'
import reducer from './reducers'

import sagas from './sagas'

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

const action = type => store.dispatch({ type })

function render() {
  ReactDOM.render(
    <Counter
      value={store.getState()}
      onIncrement={() => action('INCREMENT')}
      onDecrement={() => action('DECREMENT')}
      onIncrementAsync={() => action('INCREMENT_ASYNC')} />,
    document.getElementById('root')
  )
}

render()
store.subscribe(render)

其中的reducer代码:

export default function counter(state = 0, action) {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    case 'DECREMENT':
      return state - 1
    case 'INCREMENT_ASYNC':
      return state
    default:
      return state
  }
}

以及UI代码Counter.js文件:

import React from 'react'

const Counter = ({ value, onIncrement, onDecrement, onIncrementAsync }) =>
  <div>
    <button onClick={onIncrement}>
      增加
        </button>
    <button onClick={onIncrementAsync}>
      1s后增加
        </button>
    <button onClick={onDecrement}>
      减少
        </button>
    <hr />
    <div>
      数量: {value}
    </div>
  </div>

export default Counter

模板页:

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Redux Counter example</title>
</head>

<body>
  <div id="root"></div>
  <script type="text/javascript" src="build.js"></script>
</body>

</html>

现在的问题是 ,为什么我点击1s后增加 没有反应

阅读 2.8k
1 个回答

sagaMiddleware.run(rootSaga) 没有这个啊

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题