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 }
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.getBookingDataaccepts no parameter, which is then returning another function which is acceptingdispatchas 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 callinggetBookingData()alone is only returning an anonymous function which of course doesn't do anything if not being calledreact-reduxstuff which I'm not all too familiar with. But maybe you had mapped your function withmapDispatchToProps()?getBookingData()(dispatch)on where you commented// doesn't log 'ran getBookingData'. I think this should work