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

198
Views
reactjs passing value to useEffect from useSelector

I am building a reactjs app

I want to get my user id from redux state

using useSelector and pass it to useEffect

My code

UserReducer

export function userReducer(state = { token: "", user: {} }, action) {
  switch (action.type) {
    case "LOGIN":
      return action.payload;
    case "LOGOUT":
      return action.payload;
    default:
      return state;
  }
}

ViewTransaction.jsx

const ViewTransaction = () => {
  const user_id = useSelector((state) => state.user.user.id);
  const params = useParams();

   useEffect(() => {
      setLoading(true);
      PendingServices.getPendingTransaction(
        `localhost:8000/user/transaction/pending/get-transaction/${params.id}?user_id=${user_id}`
      )
        .then((response) => {})
        .catch((error) => {})
        .finally(() => {
          setLoading(false);
        });
    }, []);
   return (
        <div>transaction</div>
   )
}

export default ViewTransaction;

user_id is showing undefined.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to pass user_id in useEffects dependency array and check if its defined or nod... because on first render its undefined because redux didnt provided it yet

  useEffect(() => {
   if(user_id){
      setLoading(true);
      PendingServices.getPendingTransaction(
        `localhost:8000/user/transaction/pending/get-transaction/${params.id}?user_id=${user_id}`
      )
        .then((response) => {})
        .catch((error) => {})
        .finally(() => {
          setLoading(false);
        });
   }
    }, [user_id]);

about 4 years ago · Juan Pablo Isaza Report

0

Like Drew wrote in the comment above your dependency array is missing variables. React tries to be smart about not calling hooks in vain, and by passing it no dependencies you let it assume that the call to the method in useEffect will always be the same, and that it can cache and reuse the result of the invocation. Since user_id is undefined initially, when no one have logged in, the result of useEffect with an undefined user is cached.

By adding user_id and params.id in the dep array you're telling React that "each time these variables change, the result of the method should also change", so it will invalidate the cached useEffect result and run the method again.

I'd recommend using this eslint plugin to automatically help catch these cases: https://www.npmjs.com/package/eslint-plugin-react-hooks

about 4 years ago · Juan Pablo Isaza Report
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!