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

191
Views
React Redux fetch data before render and do a map function

//the website render before i fetch the data from redux and give me a error //TypeError: Cannot read properties of undefined (reading 'map')

const Home=()=> {

const dispatch = useDispatch();

//import the products list
const getProducts =  useSelector ((state) => state.getProducts);
const  { products,users, loading, error}= getProducts; //i fetch the data

const AllUsersData = [ ...(users.map(item => item.createdAt))];
 console.log(AllUsersData);

console.log(users);

useEffect(() => {
    dispatch(listProducts());
  }, [dispatch]);
  
return (
    <div className="home">
        <FeaturedInfo/>
        {loading ? (
        <h2>loading...</h2>
      ) : error ? (
        <h2>{error}</h2>
      ) : (
        <Chart data={AllUsersData} title="User Analytics" grid dataKey="Active User"/>
        )
      }
        <div className="homeWidget">
            <Widgetsmall/>
            <Widgetlg/>
        </div>
    </div>
)

}

export default Home

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

0

users is expected to be an array since you are using map function. You will need to either:

  1. Set a default value:
const  { products = [],users = [], loading, error} = getProducts;

OR

  1. Check if the value is defined; then do the map function:
const AllUsersData = users ? [ ...(users.map(item => item.createdAt))] : [];
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!