Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
How to refactor this massy lines of code into other component

I got this massy lines of codes sitting in App.js

  const { userInfo } = useSelector((state) => state.user); // JWT details
  const dispatch = useDispatch();

  const { data, loading } = useQuery(GET_USER_DETAILS);
  const { data: cartData, loading: cartLoading } = useQuery(GET_USER_CART);
  const { data: orderData, loading: orderLoading } = useQuery(GET_USER_ORDER);

  useEffect(() => {
    if (!loading && data && data?.getUserById.id === userInfo?.id) {
      dispatch(loginUser(data?.getUserById, loading));
    }
  }, [dispatch, data, loading, userInfo?.id]);

  useEffect(() => {
    if (
      !cartLoading &&
      cartData &&
      cartData?.getUserCart.userId === userInfo?.id
    ) {
      dispatch(addToCart(cartData?.getUserCart));
    }
  }, [dispatch, cartData, cartLoading, userInfo?.id]);

  useEffect(() => {
    if (
      !orderLoading &&
      orderData &&
      orderData?.getUserOrders[0]?.purchasedBy === userInfo?.id
    ) {
      dispatch(addOrder(orderData?.getUserOrders));
    }
  }, [dispatch, orderData, orderLoading, userInfo?.id]);

if you wondering why is that, everytime user is logged in I am getting the data from backend(cart data, orders data, user profile data) and put that right into my redux-store.

Is there any way to move it to another component and then just call it in App.js? Ty :D

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!