简体   繁体   English

如何在功能 typescript class 中导出方法?

[英]How to export a method in a functional typescript class?

In the code below I would like to know, how to export the function setFormData .在下面的代码中,我想知道如何导出 function setFormData In other words also I would like to know how to import setFormData from a different typescript file.换句话说,我也想知道如何从不同的 typescript 文件导入setFormData Thank you谢谢

File A文件 A

import { SyntheticEvent } from 'react';
import { useDispatch } from 'react-redux';
import { useStatusActions } from '../../actions';

export const StatusHelper = () => {
  const dispatch = useDispatch();
  let statusActions = useStatusActions(dispatch);

  const setFormData = (event: SyntheticEvent, key: any, value: any) => {
    statusActions.setFormData(key, value);
  };
  

File B文件 B

import {
  setFormData,
} from './fileA';

you will need to create a custom hook if you want to export a function that relies on another hook.如果要导出依赖于另一个钩子的 function,则需要创建一个自定义钩子。 You can't simply export and use it outside react.您不能简单地导出并在反应之外使用它。

you should have a useStatusHelper hook that returns a setFormData function你应该有一个useStatusHelper钩子,它返回一个setFormData function

check how to create custom hooks here在此处查看如何创建自定义挂钩

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM