React Component Boilerplate
Generate a React function or class component in TypeScript or JavaScript with the hooks you tick, typed props, React.memo or "use client", as a starting point. How it works
Use the React Component Boilerplate
Words are joined in PascalCase and capitals inside a word are kept. Written in the code as MyComponent.
Generated Component
import { useState } from 'react';
interface MyComponentProps {
title?: string;
}
function MyComponent({ title }: MyComponentProps) {
const [count, setCount] = useState<number>(0);
return (
<div>
{title && <h2>{title}</h2>}
<button type="button" onClick={() => setCount((current) => current + 1)}>
Count: {count}
</button>
</div>
);
}
export default MyComponent;
A starting point, not a finished component: the title, the counter and the empty effect are placeholders to replace.
About the React Component Boilerplate
With the defaults (TypeScript, a function, useState and a props interface) the generator writes function MyComponent({ title }: MyComponentProps) with a useState counter button and export default MyComponent. It is a starting point with placeholders, the optional title prop and the counter, not a finished or recommended component: tick the hooks and options you want and replace the placeholders with your own code.
Component names
| Name typed | Name in the code |
|---|---|
| my component | MyComponent |
| user-profile-card | UserProfileCard |
| x_y_z | XYZ |
| React | ReactComponent |
| HTMLDivElement | HTMLDivElementComponent |
| 2fast | Component2fast |
| 日本 | Component日本 |
| (empty) | MyComponent |
Capitals inside a word are kept. A name that starts with a digit or a letter without capitals gets the prefix Component; one that clashes with an import or the ref type gets the suffix. At most 64 characters.
The import line for each choice
| Options | Import line |
|---|---|
| Defaults | import { useState } from 'react'; |
| Class | import React from 'react'; |
| Children prop | import React, { useState } from 'react'; |
| useState, useEffect and useRef | import { useState, useEffect, useRef } from 'react'; |
| React.memo wrapper | import React, { useState } from 'react'; |
| No hooks, no props interface | (no import line) |
React is imported only where the code uses it: React.ReactNode for children, React.memo, React.Component for a class.
How to use this tool
- Type the component name; the page shows the PascalCase name it will use.
- Pick TypeScript or JavaScript and Function or Class.
- Tick the hooks (useState, useEffect, useCallback, useMemo, useRef) and the options: props interface, children prop, arrow function, React.memo wrapper, JSDoc comments, 'use client'.
- Copy the code, or Download it as a .tsx or .jsx file named after the component.
Frequently asked questions
- How do I create a React component with TypeScript?
- Keep TypeScript and Function, type the name and tick the hooks you need. The defaults give import { useState } from 'react';, interface MyComponentProps with title?: string;, the function with const [count, setCount] = useState<number>(0); and export default MyComponent;. Copy it or press Download for MyComponent.tsx.
- Can it generate a class component?
- Yes. Choose Class and the boxes add state: MyComponentState = { count: 0 } with setState, componentDidMount, a click method and React.createRef; useMemo and Arrow Function are not offered, because they have no class form.
- What does the 'use client' option do?
- It puts 'use client'; on the first line, which the Next.js App Router reads as the mark of a client component. Leave it off for plain React or for a server component.
- Why was my component name changed?
- A React component name must be a valid identifier that starts with a capital. my component becomes MyComponent, 2fast becomes Component2fast, and React becomes ReactComponent so it does not clash with the import. The page shows the name it used under the field.
- What should I change in the generated component?
- The placeholders. Every name the code declares is used, so it type-checks under TypeScript strict mode without unused-variable errors, but the optional title prop, the Count button, the empty useEffect body and the ref on the outer div only show where your code goes. Rename the props interface fields, replace the button with your markup, and delete any hook you ticked but do not need, together with its import.
Related Tools
View all toolsSVG Code Viewer
Paste SVG code to preview it as an image with scripts, event handlers and outside links removed, list its colors and elements, minify it or export React TSX.
SQL Query Formatter
Format SQL for Standard SQL, PostgreSQL, MySQL, SQLite or SQL Server, with keywords kept, upper-cased or lower-cased and 2-space, 4-space or tab indents.
User Agent Parser
Paste a User-Agent string to read the browser, engine, OS and version, device type and model, CPU hints or the crawler it names, with notes on frozen values.
JSON Diff
Compare two JSON documents side by side or as a list of paths, such as $.name changed from "John" to "Jane". Key order is ignored; numbers compare exactly.
Subnet Mask Cheat Sheet
Subnet mask table for every IPv4 prefix from /0 to /32: dotted mask, wildcard, total and usable hosts, with /31 counted as 2 (RFC 3021) and /32 as 1.
Dockerfile Generator
Generate a Dockerfile from a form: pick a language, base image, ports, env vars, build and start commands, then copy or download the file.