1

I'm new to React!

I have a function...

export const getBookingData = () => dispatch => {
  console.log('ran getBookingData');
  return new Promise(async (resolve, reject) => {

  })
}

Which I then call in another file by doing (which works fine):

import { getBookingData } from "../actions";
getBookingData(); // logs 'ran getBookingData'

However, I would like to try and call getBookingData from within the same file that it is declared.

I have tried:

const getBookingData = () => dispatch => {
  console.log('ran getBookingData');
  return new Promise(async (resolve, reject) => {

  })
}

const moveVisitor = (visitorId, destination, source) => async (dispatch, getState) => {
  console.log('error with post api'); // logs ok
  getBookingData(); // doesn't log 'ran getBookingData'

  let state = getState();
  let { entities: { booking: { booking_id } } } = state;
  let removeBed = {};
  removeBed.booking_visitor_name_id = visitorId;
  removeBed.room_id = destination;
  removeBed.booking_id = booking_id;

  api.post('/accommodation/room/move-participant', removeBed).then(function (response) {
    // ok
  }).catch(function (error) {

  });
}
export { getBookingData, moveVisitor }
6
  • getBookingData(); // doesn't work — What does "doesn't work" mean? Is there an error? Some behaviour you don't expect? Provide a clear problem statement. Commented Feb 13, 2020 at 11:39
  • Just a quick reminder that this getBookingData accepts no parameter, which is then returning another function which is accepting dispatch as its parameter. You can say that function is curried, so you may need to call it like so: getBookingData()(yourDispatchObject). What you have now by calling getBookingData() alone is only returning an anonymous function which of course doesn't do anything if not being called Commented Feb 13, 2020 at 11:53
  • @ionizer I think I only partially understand! If that were the case, then why is it that it works when called in another file ? Commented Feb 13, 2020 at 11:57
  • This seems to be some react-redux stuff which I'm not all too familiar with. But maybe you had mapped your function with mapDispatchToProps()? Commented Feb 13, 2020 at 12:00
  • 1
    Actually, try using getBookingData()(dispatch) on where you commented // doesn't log 'ran getBookingData'. I think this should work Commented Feb 13, 2020 at 12:02

2 Answers 2

2

You can say that the getBookingData function is curried, as it is a function (accepting no parameter) returning another function (accepting dispatch object as parameter). What you have by just calling getBookingData() is an anonymous function which accepts the dispatch object as the parameter, so you need to call it once more.

Replacing your non-working call of getBookingData() with getBookingData()(dispatch) should work.

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

Comments

2

Have you tried exporting as below

const getBookingData = () => dispatch => {
  return new Promise(async (resolve, reject) => {
    // some stuff here
  })
}

const moveVisitor = (visitorId, destination, source) => async (dispatch, getState) => {
  getBookingData(); // doesn't work
}

export { getBookingData, moveVisitor }

6 Comments

Although this still works in the other file it still doesn't seem to fun when invoked in the moveVisitor function
@JonathanClark It should work this way, did you double-check to see if you're using it correctly in the same file? You may have missed an argument or something like that ?
Have updated as maybe the problem wasn't where I thought
@JonathanClark So error with post api is logged, but ran getBookingData is not ?
Try console log before the promise statement, see if it works @JonathanClark
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.