Below is my code:
interface DeleteRecipeFunctionContext {
handleRecipeDelete: (id: string | number) => void;
}
const RecipeContext = createContext<DeleteRecipeFunctionContext>({
handleRecipeDelete: (id) => null, // eslint-disable-line no-unused-vars
});
createContext requires me to pass in a default function, so I am passing in a (id)=>null as a dummy placeholder before I pass in the actual function. I receive the warning from @typescript-eslint stating:
'id' is defined but never used @typescript-eslint/no-unused-vars
Writing // eslint-disable-warning/no-unused-vars
does not disable the warning, but eslint-disable-warning
does. How should I turn off this warning without turning off all warnings? On the other hand, is my approach of using createContext correct (to enter a dummy function)?
CodePudding user response:
Above your function you can add // @ts-ignore
CodePudding user response:
Disabling warnings should be a last resort option. Couple of alternatives here:
Discard the parameter:
handleRecipeDelete: (_) => null
Omit the parameter:
handleRecipeDelete: () => null
A more robust option is to make your context nullable.
const RecipeContext = createContext<DeleteRecipeFunctionContext | undefined>();
and do a null check when accessing your context, often done in a custom hook:
function useRecipeContext() { const context = useContext(RecipeContext); if (!context) { throw new Error("Missing RecipeContext.Provider value"); } return context; }