Skip to content

Instantly share code, notes, and snippets.

View ronaldruzicka's full-sized avatar

Ronald Ruzicka ronaldruzicka

  • Creative Dock
  • Prague
View GitHub Profile
@ronaldruzicka
ronaldruzicka / user-context.tsx
Last active February 9, 2021 08:57
Example of separated React context for state and updater function for better performance in Typescript
import { createContext, useContext, useState } from 'react'
type User = {
firstName: string
lastName: string
email: string
}
type Updater = (user: User) => void
@ronaldruzicka
ronaldruzicka / accordion-context.tsx
Last active February 9, 2021 08:21
Example of simple React context in Typescript
import { createContext, useContext, useState } from 'react';
type Props = {
children: React.ReactNode | React.ReactNode[];
};
type Context = {
isExpanded: boolean;
toggleItem?: () => void;
};
@ronaldruzicka
ronaldruzicka / usePrevious.ts
Last active February 5, 2021 16:07
React hook to use previous value in Typescript
import { useRef, useEffect } from 'react'
/**
* Saves previous value, if you need to compare it to a current value
* @param value - anything you need to store as a previous value
*/
export const usePrevious = <T>(value: T) => {
const ref = useRef<T>()
// Store current value in ref