当我尝试获取数据时,Redux操作将重复

发布于 2025-02-07 05:29:08 字数 1781 浏览 1 评论 0原文

我试图通过仅获取必要的日期来创建更多功能,即下一个需要添加到我在Redux Store中的现有状态的日期,但是我遇到了问题,我的Redux操作是重复的。

组件app.js

function App() {
  const dispatch = useDispatch();
  const data = useSelector(questionsData);

  useEffect(() => {
    const fetchQuestions = async () => {
      dispatch(fetchQuestionsBegin());
      try {
        const { data } = await mainUrl(`/questions?last=5`);
        return dispatch(fetchQuestionsSuccess(data));
      } catch (err) {
        return dispatch(fetchQuestionsFailure());
      }
    };
    fetchQuestions();
  }, [dispatch]);

  return (
    <>
      <div>TEST</div>
    </>
  );
}

创建商店

const store = configureStore({
  reducer: {
    questionsStore: questionsReducer,
  },
});

export default store;

slice

const initialState = {
  loading: false,
  questions: [],
  error: "",
};

const questionsSlice = createSlice({
  name: "questions",
  initialState,
  reducers: {
    fetchQuestionsBegin(state) {
      return { ...state, loading: true, error: "" };
    },
    fetchQuestionsSuccess(state, action) {
      return {
        ...state,
        loading: false,
        questions: [...state.questions, ...action.payload],
      };
    },
    fetchQuestionsFailure(state, action) {
      return { ...state, loading: false, error: action.payload };
    },
  },
});

export const { reducer: questionsReducer, actions } = questionsSlice;

export const {
  fetchQuestionsBegin,
  fetchQuestionsSuccess,
  fetchQuestionsFailure,
} = actions;

redux

当我排除react.strictmode&gt;一切都很好。

I am trying to create load more functionality by fetching only the necessary date i.e. the next one that needs to be added to the existing state that I have in the redux store, but I have a problem my redux actions are duplicated.

Component App.js

function App() {
  const dispatch = useDispatch();
  const data = useSelector(questionsData);

  useEffect(() => {
    const fetchQuestions = async () => {
      dispatch(fetchQuestionsBegin());
      try {
        const { data } = await mainUrl(`/questions?last=5`);
        return dispatch(fetchQuestionsSuccess(data));
      } catch (err) {
        return dispatch(fetchQuestionsFailure());
      }
    };
    fetchQuestions();
  }, [dispatch]);

  return (
    <>
      <div>TEST</div>
    </>
  );
}

creating store

const store = configureStore({
  reducer: {
    questionsStore: questionsReducer,
  },
});

export default store;

slice

const initialState = {
  loading: false,
  questions: [],
  error: "",
};

const questionsSlice = createSlice({
  name: "questions",
  initialState,
  reducers: {
    fetchQuestionsBegin(state) {
      return { ...state, loading: true, error: "" };
    },
    fetchQuestionsSuccess(state, action) {
      return {
        ...state,
        loading: false,
        questions: [...state.questions, ...action.payload],
      };
    },
    fetchQuestionsFailure(state, action) {
      return { ...state, loading: false, error: action.payload };
    },
  },
});

export const { reducer: questionsReducer, actions } = questionsSlice;

export const {
  fetchQuestionsBegin,
  fetchQuestionsSuccess,
  fetchQuestionsFailure,
} = actions;

redux

When I exclude <React.StrictMode> everything works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诺曦 2025-02-14 05:29:08

请参阅 link 。严格的模式可能导致多种方法调用多次。当组件首次安装时,最有可能您的REDUX跑了两次。您可以实现useref来检测初始安装,然后在

  const isMounted = useRef(false)
  useEffect(() => {
    isMounted.current = true;
  }, [])


  useEffect(() => {
    if (isMounted.current) {
       const fetchQuestions = async () => {
         dispatch(fetchQuestionsBegin());
         try {
           const { data } = await mainUrl(`/questions?last=5`);
           return dispatch(fetchQuestionsSuccess(data));
         } catch (err) {
           return dispatch(fetchQuestionsFailure());
         }
       };
       fetchQuestions();
    }
  }, [dispatch]);

Refer to link. Strict mode can cause multiple methods to invoke multiple times. Its most likely that your redux is ran twice when the component mounts for the first time. You can implement useRef to detect initial mount and then conditionally render after

  const isMounted = useRef(false)
  useEffect(() => {
    isMounted.current = true;
  }, [])


  useEffect(() => {
    if (isMounted.current) {
       const fetchQuestions = async () => {
         dispatch(fetchQuestionsBegin());
         try {
           const { data } = await mainUrl(`/questions?last=5`);
           return dispatch(fetchQuestionsSuccess(data));
         } catch (err) {
           return dispatch(fetchQuestionsFailure());
         }
       };
       fetchQuestions();
    }
  }, [dispatch]);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文