Skip to content

Instantly share code, notes, and snippets.

@D-Maher
D-Maher / useCounter.ts
Last active March 16, 2023 23:07
Custom React hook
import { useState } from "react";
function useCounter(
initialCount: number = 0
): [number, () => void, () => void] {
const [count, setCount] = useState<number>(initialCount);
function increment() {
setCount(count + 1);
}
@D-Maher
D-Maher / CounterWithHook.tsx
Last active March 16, 2023 23:02
React component that uses a custom hook
import React from "react";
import useCounter from "../hooks/useCounter";
function CounterWithHook() {
const [count, increment, decrement] = useCounter();
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
@D-Maher
D-Maher / Counter.tsx
Last active March 16, 2023 23:00
React component with local state
import React, { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
const decrement = () => {
@D-Maher
D-Maher / UserGreeting.tsx
Last active March 16, 2023 23:00
React component with props
import React from "react";
function UserGreeting({ name, age }) {
return (
<p>
Hello, my name is {name} and I am {age} years old.
</p>
);
}
@D-Maher
D-Maher / HelloWorld.tsx
Last active March 16, 2023 22:58
Simple React component
import React from "react";
function HelloWorld() {
return <h1>Hello, World!</h1>;
}
export default HelloWorld;

Keybase proof

I hereby claim:

  • I am d-maher on github.
  • I am d_maher (https://keybase.io/d_maher) on keybase.
  • I have a public key ASBi4YxoKDmPrlfcm4OMxCdNlis7JjdUa-dxqRwAAmMfHwo

To claim this, I am signing this object:

@D-Maher
D-Maher / 0_reuse_code.js
Created January 13, 2017 06:19
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console