Reputation: 1908
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)?
Upvotes: 2
Views: 4432
Reputation: 47951
To selectively disable a warning generated by a plugin rule, use the syntax
// eslint-disable-line <plugin>/<rule>
in your case:
handleRecipeDelete: (id) => null, // eslint-disable-line @typescript-eslint/no-unused-vars
I do not know if your approach to creating a context is correct, that is something that you will need to test in your application.
Upvotes: 2
Reputation: 1787
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;
}
Upvotes: 1