Open11

React写経

ikura1ikura1
ikura1ikura1

ObjectのKeyを一律Enumで列挙したい場合の書き方

type Filter = "ALL" | "Active" | "Completed"

const FILTER_MAP: { [key in Filter]: (task: TodoProps) => boolean } = {
  ALL: () => true,
  Active: (task: TodoProps) => !task.completed,
  Completed: (task: TodoProps) => task.completed,
};
ikura1ikura1
  • useRefには型を与える
  • undefinedになるので、?で対応させておく
const hogeFieldRef= useRef<HTMLInputElement>(null);

hogeFieldRef.current?.focus()