Skip to content

Instantly share code, notes, and snippets.

View Armster15's full-sized avatar
๐Ÿ‘‹
~~waves~~

Armaan A Armster15

๐Ÿ‘‹
~~waves~~
View GitHub Profile
@Armster15
Armster15 / use-media-recorder.ts
Last active June 10, 2024 05:30
A lightweight React hook for the `MediaRecorder` API that stays out of your way and makes it easy to record audio
import { useRef } from "react";
const ALL_MEDIA_RECORDER_EVENTS = [
"dataavailable",
"error",
"pause",
"resume",
"start",
"stop",
];
@Armster15
Armster15 / ReactImperativeState.tsx
Created July 6, 2024 06:39
Imperatively update state in React. Based on logic from React Hot Toast
import { useStore, updateState } from './store';
function App() {
const store = useStore();
return (
<div>
<button
onClick={() => {
updateState({
@Armster15
Armster15 / UseReducerApp.tsx
Last active July 6, 2024 07:04
A basic React `useReducer` example
import { useReducer } from 'react';
import { todoReducer, TodoActionType } from './todos';
function App() {
const [state, dispatch] = useReducer(todoReducer, []);
function handleCreateNewTodo(ev: React.FormEvent<HTMLFormElement>) {
ev.preventDefault();
const content = String(new FormData(ev.currentTarget).get('content'));
dispatch({
@Armster15
Armster15 / react-imperative-state.ts
Created July 7, 2024 01:10
React state that can be updated imperatively
import { useEffect, useState } from 'react';
export const createImperativeState = <State>(
initialState: State | (() => State)
) => {
let listeners: Array<(state: State) => void> = [];
let memoryState: State =
initialState instanceof Function ? initialState() : initialState;