dva官方实战代码 写一个列表,包含删除按钮,点删除按钮后延迟 1 秒执行删除

UI

import React, {Component} from 'react';
import { connect } from 'dva'
class List extends Component {
  constructor(props) {
    super(props);
    console.log(props)
  }
  render() {
    return (
      <div>
        {this.props.list.map((item,index) => {
          return (
            <div key={index}>{item} <button onClick={() => this.props.dispatch({type:'list/delAfter1Second',value:index})}>del</button></div>
          )
        })}
      </div>
    )
  }
}

export default connect(({list}) => ({
  list
}))(List)

model

function delay(time) {
  return new Promise((resolve,reject) => {
    setTimeout(() => {
      resolve()
    },1000)
  })
}
export default  {
  namespace: 'list',
  state: [],
  reducers: {
    del(list,action) {
      return list.filter((item,index) => index !==action.value)
    }
  },

  effects: {
    *delAfter1Second(action, { call, put }) {
      yield call(delay, 1000);
      yield put({type:'del',value:action.value});
    },
  },

}

index.js

import dva from 'dva';
import './index.css';
// 1. Initialize
const app = dva({
  initialState: {
    list: [
      '列表1',
      '列表2',
      '列表3',
      '列表4'
    ]
  }
});


// 2. Plugins
// app.use({});

// 3. Model
// app.model(require('./models/example').default);
app.model(require('./models/List').default)
// 4. Router
app.router(require('./router').default);

// 5. Start
app.start('#root');

Router.js

import React from 'react';
import { Router, Route, Switch } from 'dva/router';
import IndexPage from './routes/IndexPage';
import Counter from './components/Counter'
import List from './components/List'
function RouterConfig({ history }) {
  return (
    <Router history={history}>
      <Switch>
        <Route path="/" exact component={IndexPage} />
        <Route path="/list" exact component={List} />

      </Switch>
    </Router>
  );
}
export default RouterConfig;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值