iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

JSX Reference

JSX reference for TypeScript — typing components, props, children, events, and refs. Covers React; other frameworks (Solid, Preact, Vue) follow similar patterns.

tsconfig

tsconfig.json
{
    "compilerOptions": {
        "jsx":              "react-jsx",
        "jsxImportSource":  "react"
    }
}

Component types

TSX
type Props = { name: string; greeting?: string };

// Function declaration
function Greeter({ name, greeting = 'Hi' }: Props) {
    return <h1>{greeting}, {name}</h1>;
}

// Arrow function with React.FC
const Greeter2: React.FC<Props> = ({ name }) => <h1>Hi, {name}</h1>;

Children

TypeUse for
React.ReactNodeAnything renderable.
React.ReactElementA single JSX element.
React.JSX.ElementEquivalent to ReactElement.
React.PropsWithChildren<P>Helper that adds children to your props type.

Event types

EventTS type
onClickReact.MouseEvent<HTMLButtonElement>
onChange on inputReact.ChangeEvent<HTMLInputElement>
onSubmit on formReact.FormEvent<HTMLFormElement>
onKeyDownReact.KeyboardEvent<HTMLInputElement>
onFocus / onBlurReact.FocusEvent<HTMLInputElement>

Refs

TSX
const inputRef  = useRef<HTMLInputElement>(null);
const dialogRef = useRef<HTMLDialogElement>(null);

useEffect(() => inputRef.current?.focus(), []);

State

TSX
const [count, setCount] = useState(0);                              // number — inferred
const [user,  setUser]  = useState<User | null>(null);              // union — explicit

Props of an HTML element

TSX
type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> & {
    variant?: 'primary' | 'secondary';
};

const Button: React.FC<ButtonProps> = ({ variant = 'primary', ...rest }) => (
    <button data-variant={variant} {...rest} />
);
Tip: Don't use React.FC blindly — modern guidance is to type props directly on the function declaration and skip FC entirely. Less inference noise, no hidden children.

Example

Example
// Function components:
// type Props = { name: string };
// const Hi = ({ name }: Props) => <h1>Hi, {name}</h1>;
//
// Children: React.ReactNode
// Event:    React.MouseEvent<HTMLButtonElement>
// Ref:      React.RefObject<HTMLInputElement>
console.log('React component typing comes from @types/react');
Try it Yourself »

Exercise

Component-children prop type.

children: React.

Test yourself

Q1. Children-able prop type is…
Q2. A click handler's event type is…
Q3. A ref to an input is…

Discussion

Loading…