1

I have an initial state const item that looks like this:

const INITIAL_STATE = {
  endPoint: { street: '', coordinates: [] }, //[13.3969, 52.5182]
  startingPoint: { street: '', coordinates: [] }, //[13.413215, 52.521918]
  favouritePoint: { street: '', coordinates: [] },
  favouriteLocations: {getFavouritePlaces()},
  // favouriteLocations: [
  //   {
  //     name: 'Zu Hause',
  //     street: 'Müllersweg',
  //     coordinates: [8.217462, 53.13975], //[8.258844, 53.119525],
  //   },
  //   {
  //     name: 'Büro',
  //     street: 'Philipp-Reis-Gang',
  //     coordinates: [8.258844, 53.119525], //[8.217462, 53.13975],
  //   },
  //   {
  //     name: 'KaffeeHaus',
  //     street: 'Cloppenburgerstr.',
  //     coordinates: [8.211, 53.113],
  //   },
  // ],
  addressesFoundList: [],
};

Instead of the hardcoded value, I am trying to call a function for favouriteLocations that will return an object of the same type. However, currently this does not call my function.

My function looks like this:

const getFavouritePlaces = () => {
return ...
}

Later on, I will be using this initial state for my redux setup.

4
  • 3
    That is not valid javascript, I think favouriteLocations: {getFavouritePlaces()}, should be: favouriteLocations: getFavouritePlaces(), Commented Aug 4, 2020 at 13:23
  • Does this answer your question? How to call a constant as a function name? Commented Aug 4, 2020 at 13:32
  • Even with that, I am unable to achieve what I wanted. Since I am using a hook inside the function, when I call it here, it gives me an invalid hook error stackoverflow.com/questions/63248118/… @HMR Commented Aug 5, 2020 at 15:22
  • i don't know if this has been mentioend but did you try .appyly() Commented Aug 19, 2023 at 17:25

3 Answers 3

1

I don't think you need the brackets around the function call.

Change favouriteLocations: {getFavouritePlaces()}, To favouriteLocations: getFavouritePlaces(),.

Sign up to request clarification or add additional context in comments.

1 Comment

since I am using a hook inside the function, when I call it here, it gives me an invalid hook error stackoverflow.com/questions/63248118/…
1

As mentioned there's syntax error.

Alternativ 1 - Spreading the function containing the desired object:

const INITIAL_STATE = {
  favouriteLocations: {...getFavouritePlaces()}
};

Alternativ 2 - Run the function that will return the desired object:

const INITIAL_STATE = {
  favouriteLocations: getFavouritePlaces()
};

1 Comment

since I am using a hook inside the function, when I call it here, it gives me an invalid hook error stackoverflow.com/questions/63248118/…
0

You should declare it like this:

const getFavouritePlaces = () => {
       console.log('test');
    }
    
    const INITIAL_STATE = {
      favouriteLocations: getFavouritePlaces
    }

    INITIAL_STATE.favouriteLocations();

This will work even if your fucntion recevies a variable like:

const getFavouritePlaces = (x) => {
  console.log(x);
};

const INITIAL_STATE = {
   favouriteLocations: getFavouritePlaces
}

INITIAL_STATE.favouriteLocations('test');

2 Comments

Hi @Jnl, i edited it for you... just replace (x: string) by (x). you can use (x: string) if its a typescript code.
I meant since I am using a hook inside the function, when I call it here, it gives me an invalid hook error stackoverflow.com/questions/63248118/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.