使用默认导出功能将功能导出到另一个类

时间:2020-09-10 07:31:11

标签: javascript reactjs react-native react-redux

我有两个班级Orders.jsapi.js。我需要在ShowAlertWithDelay中调用api.js,但是由于我已经有默认导出mapStateToProps,因此我无法使用其他导出。下面是代码

Orders.js

class Order extends Component {

constructor(props) {
    super(props);
    getOrders: []
      }


ShowAlertWithDelay = () => {
ShowToast("location",3,5000);
  };

const mapStateToProps = (state) => ({
getOrders: state.User.getOrders ? state.User.getOrders : [],
deliveredorder: state.User.deliveredorder ? state.User.deliveredorder : [],
userData: state.User.userData ? state.User.userData : false,
selectedTabdeliverOrder: state.User.selectedTabdeliverOrder ? state.User.selectedTabdeliverOrder : false
});
export default connect(mapStateToProps)(Order);


api.js

class Api {
if(resp.data.RouteCompleted == 206){

//Need to call ShowAlertWithDelay(); here

}
}
export default Api;

2 个答案:

答案 0 :(得分:0)

一种简单的方法是创建一个新文件以导出ShowAlertWithDelay ShowAlertWithDelay.js

export const ShowAlertWithDelay = () => {
ShowToast("location",3,5000);
  };

在Orders.js和api.js中

import {ShowAlertWithDelay}  from './ShowAlertWithDelay.js'

当您使用“导出默认值”时,请像这样导入

import ShowAlertWithDelay  from './ShowAlertWithDelay.js'

答案 1 :(得分:0)

您可以通过在同一目录中创建index.js来为Orders.js编制索引,但是您具有独立于Orders类的“ ShowAlertWithDelay”

Orders.js

export const ShowAlertWithDelay =() =>{
  ShowToast("location",3,5000)
}
export class Order extends Component {
  //
}

index.js

export default { Orders } from './Orders.js'
export { ShowAlertWithDelay as Something } from './Orders.js'