{"id":16241454,"url":"https://github.com/niklaspor/react-ts-cheatsheet","last_synced_at":"2025-06-22T01:34:14.231Z","repository":{"id":115024790,"uuid":"184933379","full_name":"NiklasPor/react-ts-cheatsheet","owner":"NiklasPor","description":"React TypeScript Snippet Collection","archived":false,"fork":false,"pushed_at":"2019-05-05T11:32:48.000Z","size":189,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-08T09:52:33.423Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NiklasPor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-04T18:56:27.000Z","updated_at":"2020-01-31T20:09:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"faea2a47-0115-455d-a09b-05a040787fd0","html_url":"https://github.com/NiklasPor/react-ts-cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/NiklasPor/react-ts-cheatsheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasPor%2Freact-ts-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasPor%2Freact-ts-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasPor%2Freact-ts-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasPor%2Freact-ts-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NiklasPor","download_url":"https://codeload.github.com/NiklasPor/react-ts-cheatsheet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasPor%2Freact-ts-cheatsheet/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261220307,"owners_count":23126728,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T14:07:42.048Z","updated_at":"2025-06-22T01:34:09.214Z","avatar_url":"https://github.com/NiklasPor.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React-TS-Snippets\n1. [Components](#components)\n    1. [Basic](#c-basic)\n    2. [With Props](#c-with-props)\n    3. [With State and Button](#c-with-state-and-button)\n    4. [With State and Timer](#c-with-state-and-timer)\n    5. [With Child Components](#c-children)\n2. [Templating](#templating)\n    1. [If](#t-if)\n    2. [If-Else](#t-if-else)\n    3. [List](#t-list)\n    4. [Inline List](#t-inline-list)\n3. [Forms](#forms)\n    1. [Input](#f-input)\n    2. [TextArea](#f-textarea)\n    3. [Select or Dropdown](#f-select)\n\n\u003ca name=\"components\"\u003e\u003c/a\u003e\n## Components\n\n\u003ca name=\"c-basic\"\u003e\u003c/a\u003e\n### Basic\n```typescript\nclass HelloMessage extends React.Component{\n  render() {\n    return \u003ch2\u003eHello there!\u003c/h2\u003e;\n  }\n}\n```\n\n\u003ca name=\"c-with-props\"\u003e\u003c/a\u003e\n### With Props (Input Parameter)\n```typescript\nclass HelloMessage extends React.Component\u003c{userName: string}\u003e{\n  render() {\n    return \u003ch2\u003eHello user: {this.props.userName} !\u003c/h2\u003e;\n  }\n}\n```\n\n\u003ca name=\"c-with-state-and-button\"\u003e\u003c/a\u003e\n### With State and Button\n```typescript\nclass Ticker extends React.Component\u003c{start: number, modifier: number}, {counter: number}\u003e {\n  private timer!: NodeJS.Timeout;\n\n  constructor(props: {start: number, modifier: number}) {\n    super(props);\n    this.state = {counter: props.start};\n    this.onClick = this.onClick.bind(this);\n  }\n\n  onClick(): void {\n    this.setState((state, props) =\u003e ({counter: state.counter + props.modifier}))\n  }\n\n  render() {\n    return (\n      \u003cdiv\u003e\n        \u003ch1\u003eActually, I can count: { this.state.counter }\u003c/h1\u003e\n        \u003cbutton onClick={this.onClick}\u003eAdd {this.props.modifier}!\u003c/button\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n\u003ca name=\"c-children\"\u003e\u003c/a\u003e\n### Child Components or Containment\n```typescript\nclass App extends React.Component {\n  render() {\n    return (\n    \u003cdiv\u003e\n      \u003cSidebar\u003e\n        \u003cUserMenu/\u003e\n        \u003cGeneralMenu/\u003e\n      \u003c/Sidebar\u003e\n    \u003c/div\u003e\n    );\n  }\n}\n\nclass Sidebar extends React.Component {\n  render() {\n    return (\n      \u003cdiv\u003e\n        { this.props.children }\n      \u003c/div\u003e\n    );\n  }\n}\n\nclass GeneralMenu extends React.Component {\n  render() {\n    return (\n      \u003cdiv\u003e\n        \u003ch2\u003eGeneral\u003c/h2\u003e\n        \u003cul\u003e\n          \u003cli\u003eMessages\u003c/li\u003e\n          \u003cli\u003eGroups\u003c/li\u003e\n          \u003cli\u003eConfiguration\u003c/li\u003e\n        \u003c/ul\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n\nclass UserMenu extends React.Component {\n  render() {\n    return (\n      \u003cdiv\u003e\n        \u003ch2\u003eUser\u003c/h2\u003e\n        \u003cul\u003e\n          \u003cli\u003eName\u003c/li\u003e\n          \u003cli\u003eBirthday\u003c/li\u003e\n          \u003cli\u003eCountry\u003c/li\u003e\n        \u003c/ul\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n\u003ca name=\"c-with-state-and-timer\"\u003e\u003c/a\u003e\n### With State and Timer\n```typescript\nclass Ticker extends React.Component\u003c{start: number, modifier: number}, {ticker: number}\u003e {\n  private timer!: NodeJS.Timeout;\n\n  constructor(props: {start: number, modifier: number}) {\n    super(props);\n    this.state = {ticker: props.start};\n  }\n\n  componentDidMount() {\n    this.timer = setInterval(\n      () =\u003e this.setState((state, props) =\u003e ({ticker: state.ticker + props.modifier})),\n      250\n    )\n  }\n\n  componentWillUnmount() {\n    clearInterval(this.timer)\n  }\n\n  render() {\n    return (\n      \u003cdiv\u003e\n        \u003ch1\u003eActually, I can count: { this.state.ticker}\u003c/h1\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n\u003ca name=\"templating\"\u003e\u003c/a\u003e\n## Templating\n\n\u003ca name=\"t-if\"\u003e\u003c/a\u003e\n### If\n```typescript\nclass Joke extends React.Component\u003c{answer: boolean}\u003e {\n  render() {\n    return (\n      \u003cdiv\u003e\n        \u003ch1\u003eKnock Knock!\u003c/h1\u003e\n        { this.props.answer \u0026\u0026\n        \u003ch2\u003eWho is there??\u003c/h2\u003e\n        }\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n\u003ca name=\"t-if-else\"\u003e\u003c/a\u003e\n### If-Else (Regular Ternary Operator)\n```typescript\nclass Joke extends React.Component\u003c{angry: boolean}\u003e {\n  render() {\n    return (\n      \u003cdiv\u003e\n        \u003ch1\u003eKnock Knock!\u003c/h1\u003e\n        { this.props.angry\n          ? \u003ch1\u003eLeave me alone!!\u003c/h1\u003e\n          : \u003ch2\u003eYes...?\u003c/h2\u003e\n        }\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n\u003ca name=\"t-list\"\u003e\u003c/a\u003e\n### Inline List\n```typescript\nclass Numbers extends React.Component\u003c{numbers: number[]}\u003e {\n  render() {\n    return (\n      this.renderNumbers()\n    );\n  }\n\n  renderNumbers() {\n    return this.props.numbers.map((n) =\u003e\n      \u003cspan key={n.toString()}\u003e { n } \u003c/span\u003e\n    );\n  }\n}\n```\n\n\u003ca name=\"t-inline-list\"\u003e\u003c/a\u003e\n### List\n```typescript\nclass Numbers extends React.Component\u003c{numbers: number[]}\u003e {\n  render() {\n    return (\n      \u003cdiv\u003e\n        { this.props.numbers.map((n) =\u003e \n          \u003cspan key={n.toString()}\u003e { n } \u003c/span\u003e\n        )}\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n\u003ca name=\"forms\"\u003e\u003c/a\u003e\n## Forms\n\n\n\u003ca name=\"f-input\"\u003e\u003c/a\u003e\n### Input\n```typescript\nclass InputTextSync extends React.Component\u003c{}, {text: string}\u003e {\n  constructor(props: {}) {\n    super(props)\n    this.state = {text: 'Start Text'}\n    this.onChange = this.onChange.bind(this);\n  }\n\n  onChange(event: FormEvent\u003cHTMLInputElement\u003e) {\n    this.setState({text: event.currentTarget.value});\n  }\n\n  render() {\n    return (\n      \u003cdiv\u003e\n        \u003ch1\u003e{ this.state.text }\u003c/h1\u003e\n        \u003cinput value={this.state.text} onChange={this.onChange}\u003e\u003c/input\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n\u003ca name=\"f-textarea\"\u003e\u003c/a\u003e\n### TextArea\n```typescript\nclass TextAreaSync extends React.Component\u003c{}, {text: string}\u003e {\n  constructor(props: {}) {\n    super(props)\n    this.state = {text: 'Start Text'}\n    this.onChange = this.onChange.bind(this);\n  }\n\n  onChange(event: FormEvent\u003cHTMLTextAreaElement\u003e) {\n    this.setState({text: event.currentTarget.value});\n  }\n\n  render() {\n    return (\n      \u003cdiv\u003e\n        \u003ch1\u003e{ this.state.text }\u003c/h1\u003e\n        \u003ctextarea value={this.state.text} onChange={this.onChange}\u003e\u003c/textarea\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n\u003ca name=\"f-select\"\u003e\u003c/a\u003e\n### Select or Dropdown\n```typescript\nclass FavoriteColor extends React.Component\u003c{}, {selected: string}\u003e {\n  constructor(props: {}) {\n    super(props)\n    this.state = {selected: 'red'}\n    this.onChange = this.onChange.bind(this);\n  }\n\n  onChange(event: FormEvent\u003cHTMLSelectElement\u003e) {\n    this.setState({selected: event.currentTarget.value});\n  }\n\n  render() {\n    return (\n      \u003cdiv\u003e\n        \u003ch1\u003e{ this.state.selected }\u003c/h1\u003e\n        \u003cselect value={this.state.selected} onChange={this.onChange}\u003e\n            \u003coption value=\"red\"\u003eRed\u003c/option\u003e\n            \u003coption value=\"green\"\u003eGreen\u003c/option\u003e\n            \u003coption value=\"blue\"\u003eBlue\u003c/option\u003e\n            \u003coption value=\"black\"\u003eBlack is not a color.\u003c/option\u003e\n          \u003c/select\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniklaspor%2Freact-ts-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fniklaspor%2Freact-ts-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniklaspor%2Freact-ts-cheatsheet/lists"}