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

182
Views
reactjs set hook method is not working after console.log

I want to set filtredMenu with items of full "menu" data by useEffect but when I console.log it, it gives null. When I do console.log(menu), it works without a problem, I see all menu items in the console. I will filter the filtredMenu by category buttons later. What am I doing wrong?

This is my document:

import { useContext, useEffect, useState } from "react";
import MenuContext from "../context/MenuContext";


function Menu() {

    const { menu, setMenu } = useContext(MenuContext);
    const [filtredMenu, setFiltredMenu] = useState(null);

    useEffect(() => {
        setFiltredMenu(menu);
        console.log(filtredMenu)
    }, []);

    function handleMenu(e) {
        e.target.value !== "all"
            ? setFiltredMenu(menu.filter(item => item.category === e.target.value))
            : setFiltredMenu(menu);
    }

    return (

        <>
            {/* {filtredMenu.map((item) => (
                    <>
                        <button key={item.id} value={item.category} onClick={handleMenu}>
                            {item.category}
                        </button>
                    </>
                ))} */}

            {menu.map((item) =>
                <div key={item.id}>
                    <div>
                        {item.title}
                    </div>
                    <div>
                        {item.category}
                    </div>
                    <div>
                        {item.price}
                    </div>
                    <div>
                        {item.desc}
                    </div>
                    <img src={item.src} alt="image" />
                </div>
            )}
        </>

    )
}

export default Menu

Thank you.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This is because React's setState functions are async, you won't see the state change right away. If you would wait a second you would see the value change.

about 4 years ago · Santiago Trujillo 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!