简体   繁体   English

我如何调用/执行外部功能组件的 function 反应?

[英]How can i call/execute function of functional component outside react?

I have a custom component called Loader :我有一个名为Loader的自定义组件:

export const ModalLoader = props=>{
  const {
    loading,
    ...attributes
  } = props;

  const [isModalOpened, setModal] = useState(props)
  const closeModal = ()=>{
    setModal({loading:false})
  }

  return(
    <Modal
      transparent={true}
      animationType={'none'}
      visible={isModalOpened.loading}
    >
      //my Loader view
    </Modal>
  )
}

i want to execute closeModal() function in axios instance, because of my axios is not a react component , i got an error when i tried this code:我想在axios实例中执行closeModal() function,因为我的axios不是这个react component ,我在尝试时error

AxiosHttp.interceptors.response.use((res)=>{
  //tried to call closeModal()
  ModalLoader.closeModal()
  CustomLog(res.data, 'Interceptor')
  return res.data;
},(err)=>{
  CustomLog(err, 'Interceptor Error')
  if(err.message == 'Network Error'){
  return Promise.reject(err)
})

export default AxiosHttp

error detail:错误详情:

在此处输入图像描述

is there a way to call closeModal() from axios instance?有没有办法从axios实例调用closeModal()

I would suggest using context.我建议使用上下文。 Then you can use in any other component然后你可以在任何其他组件中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问一个功能组件 state 之外的组件? - 反应 - How can I access a functional components state outside the component? - React 在React上,如何在功能/无状态组件上调用组件安装上的函数? - On React, how can I call a function on component mount on a functional/stateless component? React + TS:如何从 React 功能组件外部调用方法 - React + TS: How to call a method from outside of a React Functional Component 如何在reactjs中调用无状态功能组件内部的函数 - How can I call a function inside of stateless functional component in reactjs 如何调用 function 在 React Native 的自定义组件中设置外部变量? - How can I call a function that set sets an outside variable inside a custom component in React Native? 为什么我不能从这个功能组件调用 React 钩子? - Why can't I call a React hook from this functional component? 反应 | 在组件外调用 function - React | Call a function outside a component 如何检测React功能组件之外的点击 - How to detect click outside of the React functional component 如何使用带有钩子的反应功能组件测试句柄 function 调用 - How to test a handle function call with a react functional component with hooks 如何将此 Class 组件 function 转换为 React Native 中的功能组件 - How can I convert this Class component function into functional component in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM