Dva: models下面的Effects和Reducers问题

Created on 21 Nov 2016  ·  7Comments  ·  Source: dvajs/dva

  // 表单属性
  const fromProps = {
    yangben,tanxingbi,hangqing,lazhu,kzd,
    onSearch(fieldsValue) {
      dispatch({
        type: 'tunmo/showModal',
        payload: {
          modalType: 'create',
        },
      });
    }
  };

  // 渲染
  return (
    <div>
      <Layout location={location}>
          <Row>
              <Col span={5} className={styles.rightBorder}>
                    <From {...fromProps}/>
              </Col>
              <Col span={19} className={styles.connect}>
                    <ChartShape height={490}/>
              </Col>
          </Row>
      </Layout>
    </div>
  );

通过一些文档了解,Effects大约是做异步处理的,Reducers是做同步处理的
这里有个疑问,什么时候用Effects,什么时候用Reducers呢?
调用方式有何不同?

question

Most helpful comment

没有不同,都是通过 dispatch(Action)

All 7 comments

修改 state 时用 Reducer,其他情况都用 Effect 。

那还请问下,调用方式有啥不同吗?

没有不同,都是通过 dispatch(Action)

    effects: {
        *create(){},
    },
    reducers: {
        create(){}
    }

那我这样写,dispatch(users/create) 合法?会调用那个呢?

两个都调。

备注:在dva@2后,只有effect会调用,所以reducer和effect的方法不能同名。 #1186

Was this page helpful?
0 / 5 - 0 ratings