dva icon indicating copy to clipboard operation
dva copied to clipboard

移动端的dva,在界面加载完,怎样发出获取数据的请求?

Open bjwulin opened this issue 10 years ago • 9 comments

在web,@sorrycc建议在subscriptions用history.listen(location => {})来发出获取数据的请求,这样的话,组件可以设计为stateless function。那么在reactnative,由于没有用router,这个获取数据怎么写呢?

bjwulin avatar Sep 30 '16 03:09 bjwulin

直接 dispatch action 触发 effect 就好了

sorrycc avatar Sep 30 '16 04:09 sorrycc

@sorrycc ,在什么时候dispatch呢?就在stateless function的方法里?

bjwulin avatar Sep 30 '16 04:09 bjwulin

在stateless function里面写获取数据会被循环执行。

bjwulin avatar Sep 30 '16 04:09 bjwulin

在 subscriptions 里 dispatch 。

sorrycc avatar Sep 30 '16 05:09 sorrycc

subscriptions: {
    setup({dispatch, history}) {
    dispatch({
                type: 'getInitData', 
     });
    }
}

这样么?

bjwulin avatar Sep 30 '16 05:09 bjwulin

对。

sorrycc avatar Sep 30 '16 05:09 sorrycc

@sorrycc ,我试过,不行,subscriptions应该是在app.model()时就触发,但是我是需要每次组件加载时才出发,比如有些组件是需要登录之后才能访问的。 我觉得只有componentWillMount是最好的位置,但是stateless function没有生命周期。另外,我也希望这个界面销毁后,对应model中的一部分数据销毁,在dva for rn中更不好实现。

bjwulin avatar Oct 08 '16 02:10 bjwulin

需求是组件加载时触发的话,那就只能写在 componentWillMount 里了,不要用 stateless function,stateless function 不是必须的。

sorrycc avatar Oct 08 '16 02:10 sorrycc

好的,3ks。

bjwulin avatar Oct 08 '16 03:10 bjwulin