Giter VIP home page Giter VIP logo

dvajs_todolist's Introduction

dvajs

1. 项目结构

├── node_modules
├── package.json
├── public
|  ├── favicon.ico
|  ├── index.html
|  ├── logo192.png
|  ├── logo512.png
|  ├── manifest.json
|  └── robots.txt
├── README.md
├── src
|  ├── components
|  |  ├── Addtodo.js
|  |  └── TodoListItem.js
|  ├── index.js
|  ├── models
|  |  ├── count.js
|  |  └── todos.js
|  ├── router.js
|  └── routes
|     ├── Count.js
|     └── Todos.js
└── yarn.lock

2. 入口index.js

import dva from "dva";
import router from "./router";

const allModel = require.context('./models', true, /\.js$/);

// 1.init
const app = dva();

// 2.定义Model
// app.model(require("./models/todos").default);
allModel.keys().forEach((key) => {
  // console.log("allModel(key).default", allModel(key).default);
  app.model(allModel(key).default);
});

// 3.Router
app.router(router);
// 4.Start
app.start("#root");

3. 路由router.js

import { Route, Router, Switch, Link } from "dva/router";
import Todos from "./routes/Todos";
import Count from './routes/Count';

function RouterConfig({ history }) {
  return (
    <Router history={history}>
      <>
        <Link to="/">home</Link> | <Link to="/count">count</Link>
        <Switch>
          <Route path="/" exact component={Todos} />
          <Route path="/count" exact component={Count} />
        </Switch>
      </>
    </Router>
  );
}

export default RouterConfig;

4. models > count模型

const delay = (time) => {
  return new Promise((resolve) => {
    setTimeout(() => resolve(), time);
  });
};

const countStore = {
  namespace: "count",
  state: {
    count: 1,
  },
  reducers: {
    add(state, { payload }) {
      return { count: state.count + (payload?.num || 1) };
    },
    minus(state) {
      return { count: state.count - 1 };
    },
  },
  effects: {
    // 异步添加
    *asyncAdd({ payload }, effect) {
      const { call, put } = effect;

      yield call(delay, 1000);
      yield put({ type: "add", payload });
    },
  },
  // 订阅
  subscriptions: {
    setup(props) {
      const { history, dispatch } = props;
      return history.listen(({ pathname }) => {
        if (pathname === "/count") {
          alert("subscriptions");
          dispatch({
            type: "asyncAdd",
            payload: {
              num: 1,
            },
          });
        }
      });
    },
  },
};

export default countStore;

5. routes > Count页面

import React from "react";
import { connect } from "dva";

const Count = (props) => {
  const { count, add, minus, asyncAdd } = props;

  return (
    <div>
      count: {count} <br />
      <button onClick={add}>add</button>
      <button onClick={minus}>minus</button>
      <button onClick={() => asyncAdd(10)}>asyncAdd +10</button>
    </div>
  );
};

const mapStateToProps = (state) => state.count;

const mapDispatchToProps = (dispatch) => ({
  add() {
    dispatch({
      type: "count/add",
    });
  },
  minus() {
    dispatch({
      type: "count/minus",
    });
  },
  asyncAdd(num) {
    dispatch({
      type: "count/asyncAdd",
      payload: {
        num,
      },
    });
  },
});

export default connect(mapStateToProps, mapDispatchToProps)(Count);

dvajs_todolist's People

Contributors

srect avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.