V
V
Vladimir2020-05-23 05:51:33
React
Vladimir, 2020-05-23 05:51:33

Why does the component not see the menu?

Good morning. Please tell me why I can't sort through the menu which I kind of wrote down in the state, but it's empty..

export default function GetNewTender() {

    let [nav, setNav] = useState();

    const getMenu = async() => {

        let menu = await fetch('http://localhost:4000/menu', {method: 'post'});
        menu = await menu.json();

        return menu

    };

    useEffect(() => {
        setNav(getMenu());
    });

    return (
                    <ul className='select'>
                        <li> Выберите тип {nav.toString()}</li>
                        <div className="selectContent">
                            {nav.map(el => (<li onClick={(e) => selectItem(e)} key={el.id}>{el.name}</li>))}
                        </div>
                    </ul>
    )
}


Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-05-23
@HistoryART

setNav(getMenu());

will write there a promise that getMenu will return. No one will be waiting for your fetch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question