在同一文件中反应调用导出功能

时间:2020-02-13 11:37:03

标签: javascript reactjs

我是React的新手!

我有一个功能...

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

  })
}

然后通过执行以下操作调用另一个文件

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

但是,我想尝试在声明的相同文件中调用getBookingData

我尝试过:

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 }

2 个答案:

答案 0 :(得分:2)

您是否尝试过如下导出

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 }

答案 1 :(得分:1)

您可以说getBookingData函数是咖喱函数,因为它是一个函数(不接受任何参数)返回另一个函数(接受调度对象作为参数)。只需调用getBookingData()便拥有一个匿名函数,该函数接受调度对象作为参数,因此您需要再次调用它。

getBookingData()的无效电话替换为getBookingData()(dispatch)应该可以。