《ant design pro v5 获取动态菜单与基于角色权限管理视频教程》 和 《零基础学习 Vue JS 3 完全免费视频教程》 正在更新

世界上最伟大的投资就是投资自己的教育

剧场模式
首页前端Redux轻松学 Redux-Saga 视频教程

轻松学 Redux-Saga #3 Redux-Saga 的安装和输写第一个 Saga

求知小风 · 元婴发布于
5

src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

import { createStore, applyMiddleware } from 'redux';
import rootReducer from './reducers';
import { composeWithDevTools } from 'redux-devtools-extension';
import createSagaMiddleware from 'redux-saga';

import { Provider } from 'react-redux';
import { helloSaga } from './sagas';

const sagaMiddleware = createSagaMiddleware();

const store = createStore(
  rootReducer,
  composeWithDevTools(
    applyMiddleware(sagaMiddleware)
  )
);

sagaMiddleware.run(helloSaga);

ReactDOM.render(
  <Provider store={ store }>
    <App />
  </Provider>,
  document.getElementById('root')
);

registerServiceWorker();

src/app.js

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { connect } from 'react-redux';
import { increment } from './actions/counter';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <p className="App-intro">
          { this.props.counter }
        </p>
        <p>
          <button onClick={ this.props.increment }>+</button>
        </p>
      </div>
    );
  }
}

const mapStateToProps = (state) => {
  return {
    counter: state.counter
  };
};

export default connect(mapStateToProps, { increment })(App);

src/reducers/counter.js

import { INCREMENT } from '../constants/counter';

const counter = (state = 1, action = {}) => {
  switch(action.type) {
    case INCREMENT:
      return state + 1;
    default: return state;
  }
}

export default counter;

src/reducers/index.js

import { combineReducers } from 'redux';

import users from './users';
import counter from './counter';

export default combineReducers({
  users,
  counter
});

src/actions/counter.js

import { INCREMENT } from '../constants/counter';

export const increment = () => {
  return {
    type: INCREMENT
  }
};

src/constants/counter.js

export const INCREMENT = 'INCREMENT';

src/sagas/index.js

export function* helloSaga() {
  console.log('Hello Saga!');
}
8 条回复
热门课程 love
喜欢
微信群讲师微信知乎服务号github视频号
求知编程微信群
学员(215)
quantum · 太乙xo  · 太乙wdy · 合体fruitsdrink · 道祖破妄之眼 · 太乙Tony · 元婴plutozzh · 真仙平庸患者 · 元婴vaveee@qq.com · 道祖郑宋君 · 元婴JesusCracker · 道祖15640518366 · 真仙
最新动态
quantum · 太乙学习到了8:37
xo  · 太乙学习到了4:18
wdy · 合体学习到了6:40
fruitsdrink · 道祖学习到了7:31
破妄之眼 · 太乙学习到了8:37
统计信息
    学员: 19799
    视频数量: 1240
    帖子数量: 443

© 汕尾市求知科技有限公司 | 关注我们 | 在线学员:1122

粤公网安备 44152102000088号 | 粤ICP备19038915号

Top