Jonathan Clark
Jonathan Clark

Reputation: 1260

React call export function in same file

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 }

Upvotes: 1

Views: 1013

Answers (2)

ionizer
ionizer

Reputation: 1721

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.

Upvotes: 2

Akshay
Akshay

Reputation: 14378

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 }

Upvotes: 2

Related Questions