Skip to content

Instantly share code, notes, and snippets.

@createdbymahmood
Last active November 30, 2020 09:08
Show Gist options
  • Save createdbymahmood/e0911ef620c45d0a46e2966b5241a948 to your computer and use it in GitHub Desktop.
Save createdbymahmood/e0911ef620c45d0a46e2966b5241a948 to your computer and use it in GitHub Desktop.
How to render input/textarea based on some conditions
function isPropsForTextareaElement(
props: InputProps | TextareaProps
): props is TextareaProps {
return "rows" in props;
}
export const TextareaInputGeneralComponent = (
props: InputProps | TextareaProps
) => {
if (isPropsForTextareaElement(props)) {
return <textarea {...props} />;
} else {
return <input {...props} />;
}
}; // optionally use a custom type guard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment