0

I'am working on a react Js project and i have a problem.

I try to call a function from the back (which works well) but it does not read my function and does not display the prints of my function.

There is: my function:


const getEvents = async() => {
    console.log("PLZ")
    const test = await getAllEvents();
    console.log(test)

    
}

export const Calendar = () => {
    return (
        <StyledCalendar>
        {console.log("AHHHHHHH")}
        {getEvents}
           </StyledCalendar>
    )}

I shortened the return to make it easier to read.

and there is my calls function:

export const getAllEvents = async() =>
    await axios.post(`${SERVER_URL}/calendar/getAllEvent `, {
    });

It doesn't print(the console.log(PLZ)), but it print the console.log(AHHHHHHH) Thanks for yours answers!

guiguilecodeur
  • 429
  • 2
  • 15
  • `getEvents` does not *call* the function, `getEvents()` does. Although be aware that `getEvents()` will give you back a promise. – VLAZ May 08 '22 at 10:49
  • 1
    And the promise returned by `getEvents()` resolves to `undefined` because there is no `return` statement. – Quentin May 08 '22 at 10:50
  • And you can't use a promise as the value of a `{...}` expression in JSX in React. See the [question VLAZ linked](https://stackoverflow.com/questions/15886272/what-is-the-difference-between-a-function-call-and-function-reference) for the thing about the code not calling `getEvents`, and I've added several more about using the results of an asynchronous function. – T.J. Crowder May 08 '22 at 10:51
  • you should look into direction for getting the data in `useEffect` hook and store in state to be further rendered via `Calendar` component. – user1672994 May 08 '22 at 10:52

0 Answers0