与打字稿类型反应钩子

时间:2020-03-30 13:27:07

标签: reactjs typescript

我想编写一个带有useEffect的自定义react钩子和带有typescript的useState来获取组件道具。我想显示未完成的任务。我有以下道具

export interface Props {
  lists: List[];
  tasks: Task[];
 }
 export interface List {
  id: string;
  title: string;
 }
 export interface Task {
  id: string;
  ref: string;
  listId: string;

我是个很新的反应者,但这就是我试图获得那些我怀疑缺少一些东西但被卡住的道具的方法!

type = Hook (TaskID: string) => string// this goes above the props as part of the solution.


    const [lists, setLists] = useState([]);
    const [openedTask, setOpenedTask] = useState(null);

     const useTaskId: Hook = (TaskID) => {
     function openTasks(list: Task) {
         setOpenedTask(list.title)
     }

     useEffect(() => {
         URL.subscribeToTaskId(TaskID, openTasks);

         return () => {
             URL.unsubscribeFromTaskId(TaskID, openTasks);
         };
     });

     return openedTask;
 }

0 个答案:

没有答案