• redux例子


    redux同步例子

    // App.js
    import React from "react";
    import ReduxPage from "./pages/ReduxPage.js";
    
    function App() {
      return (
        <div className="App">
          <ReduxPage />
        </div>
      );
    }
    
    export default App;
    
    // store/index.js
    import { createStore } from "redux";
    // 定义修改规则
    function countReducer(state = 0, action) {
      switch (action.type) {
        case "ADD":
          return state + 1;
        case "MINUS":
          return state - 1;
        default:
          return state;
      }
    }
    const store = createStore(countReducer);
    export default store;
    
    // pages/ReduxPage.js
    import React, { Component } from "react";
    import store from "../store/index";
    
    export default class ReduxPage extends Component {
      componentDidMount() {
          // 订阅
        store.subscribe(() => {
          this.forceUpdate();
        });
      }
      add = () => {
          // 派发 
        store.dispatch({ type: "ADD" });
      };
      minus = () => {
        store.dispatch({ type: "MINUS" });
      };
      render() {
          // 获取数据
        return (
          <div>
            <h3>ReduxPage</h3>
            <p>{store.getState()}</p>
            <button onClick={this.add}>add</button>
            <button onClick={this.minus}>minus</button>
          </div>
        );
      }
    }

    redux的dispatch只支持传入对象,若想实现更复杂的内容,如传入函数,实现异步操作,请求接口获取数据等,可以选择借助中间件增强其功能,如redux-thunk

    // store/index.js
    import { createStore, applyMiddleware } from "redux";
    import thunk from "redux-thunk";
    import logger from "redux-logger";
    // 定义修改规则
    function countReducer(state = 0, action) {
      switch (action.type) {
        case "ADD":
          return state + 1;
        case "MINUS":
          return state - 1;
        default:
          return state;
      }
    }
    
    const store = createStore(countReducer, applyMiddleware(thunk, logger));
    export default store;
    
    // ReduxPage.js
    import React, { Component } from "react";
    import store from "../store/index";
    
    export default class ReduxPage extends Component {
      componentDidMount() {
        store.subscribe(() => {
          this.forceUpdate();
        });
      }
      add = () => {
        store.dispatch({ type: "ADD" });
      };
      minus = () => {
        store.dispatch({ type: "MINUS" });
      };
      asyAdd = () => {
        store.dispatch((dispatch) => {
          console.log("123");
          setTimeout(() => {
            dispatch({ type: "ADD" });
          }, 1000);
        });
      };
    
      render() {
        return (
          <div>
            <h3>ReduxPage</h3>
            <p>{store.getState()}</p>
            <button onClick={this.add}>add</button>
            <button onClick={this.minus}>minus</button>
            <button onClick={this.asyAdd}>asyAdd</button>
          </div>
        );
      }
    }
    

    ps:感谢&参考 各路大神

      

  • 相关阅读:
    ( 转)移动端H5页面之iphone6的适配
    谨慎设置iScroll4的useTransform属性,他会导致scrollToElement方法表现异常
    (转)配置Apache服务器,使浏览器访问无缓存
    html DIV元素左右偏移方法,偏移后默认宽度仍浏览器宽度一致
    Content Security Policy(CSP)简介(转)
    隐式打开Activity——Intent设置(如何打开)和Intent-fileter配置(怎么能被打开)
    最近使用iScroll遇到的一些问题及最后的解决方法
    (转)CSS3 Media Queries
    自定义checkbox和radio
    三只松鼠卖坚果
  • 原文地址:https://www.cnblogs.com/haimengqingyuan/p/13397242.html
Copyright © 2020-2023  润新知