简体   繁体   English

如何停止调度请求操作?

[英]How can I stop the dispatch request action?

i'm using react-native with redux-saga我正在使用 react-native 和 redux-saga

I am dispatching a FLFOOD_UP_REQUEST by using useEffect in the initial rendering on the FollowFood page.我在 FollowFood 页面的初始渲染中使用 useEffect 来调度 FLFOOD_UP_REQUEST。

However, if I go back to another page before it becomes FLFOOD_UP_SUCCESS, FLFOOD_UP_REQUEST does not stop and it becomes FLFOOD_UP_SUCCESS later, causing an error.但是,如果我 go 在它变为 FLFOOD_UP_SUCCESS 之前回到另一个页面,则 FLFOOD_UP_REQUEST 并没有停止,它稍后变为 FLFOOD_UP_SUCCESS,从而导致错误。 If FLFOOD_UP_REQUEST is executed and moves to another page, then FLFOOD_UP_SUCCESS succeeds and the page screen is not shown.如果执行 FLFOOD_UP_REQUEST 并移动到另一个页面,则 FLFOOD_UP_SUCCESS 成功并且页面屏幕不显示。

Therefore, what I want to do is that even if FLFOOD_UP_REQUEST is executed due to useEffect, if I move another page before FLFOOD_UP_SUCCESS, FLFOOD_UP_REQUEST will stop and I hope that FLFOOD_UP_SUCCESS will not be executed.因此,我要做的是,即使由于useEffect执行了FLFOOD_UP_REQUEST,如果我在FLFOOD_UP_SUCCESS之前移动另一个页面,FLFOOD_UP_REQUEST会停止,我希望FLFOOD_UP_SUCCESS不会被执行。

this is my code这是我的代码

(FollowFood.js) (FollowFood.js)

    const FollowFood = () => {
      useEffect(() => {
        dispatch({
          type: FLFOOD_UP_REQUEST,
        });
      }, []);

      return (
        <>
          <FlatList
            keyExtractor={(item) => String(item.id)}
            onEndReached={() => {
              EndReached();
            }}
            onEndReachedThreshold={2}
            refreshing={loading}
            renderItem={({item}) => (
              <ImageContainer>
                <CardFollow item={item} Follow={Follow} />
              </ImageContainer>
            )}
            ListEmptyComponent={<LoadingFood label={'no.'} />}
          />
        </>
      );
    };

    export default FollowFood;

(saga.js) (saga.js)

    function* flfoodPost(action) {
      try {
        const result = yield call(flfoodUpAPI, action.data);
        yield put({
          type: FLFOOD_UP_SUCCESS,
          data: result.data,
        });
      } catch (err) {
          yield put({
            type: FLFOOD_UP_FAILURE,
            error: err.response.data,
          });
        }
      }

What is the error that you get?你得到什么错误? You can't render something?你不能渲染一些东西?

Need more info about if FLFOOD_UP_REQUEST is called from other screen, but based on info given here you can use a variable FLFOOD_COMPLETED_IN_PAGE and discard SUCCESS if that didn't complete from that page.需要有关是否从其他屏幕调用FLFOOD_UP_REQUEST的更多信息,但根据此处给出的信息,您可以使用变量FLFOOD_COMPLETED_IN_PAGE并在该页面未完成时丢弃SUCCESS

To check if completed in same page you can have other variables:要检查是否在同一页面中完成,您可以使用其他变量:

  1. fflood_requested = false
  2. fflood_request_over = false
  3. fflood_request_status = true
  4. FLFOOD_COMPLETED_IN_PAGE = false

In FollowFood.js now you can listen for these variable change if user changed screen states will not be in proper status.现在,在FollowFood.js中,如果用户更改的屏幕状态不会处于正确状态,您可以监听这些变量更改。

Note: you can also have dipatch FFLOOD_REQUEST_DOWN to reset states, when you get back response.注意:当您收到响应时,您还可以让 dipatch FFLOOD_REQUEST_DOWN重置状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM