Skip to content

Instantly share code, notes, and snippets.

View ljaviertovar's full-sized avatar
👽
Programming the universe::.

L Javier Tovar ljaviertovar

👽
Programming the universe::.
View GitHub Profile
@ljaviertovar
ljaviertovar / useAutocomplete.ts
Created October 6, 2022 03:07
Custom hook to autocomplete search
import { useEffect, useState } from "react";
import { Country } from "../ts/interfaces/Country.interface";
const useAutocomplete = (
data: Country[],
inputSearchRef: HTMLInputElement | null
) => {
const [searchedValue, setSearchedValue] = useState("");
const [suggestions, setSuggestions] = useState < Country[] > [];
@ljaviertovar
ljaviertovar / Autocomplete.tsx
Created October 6, 2022 03:03
An autocomplete search component
import { useEffect, useRef } from "react";
import { Card, Col, Input, Row, Text, User } from "@nextui-org/react";
import { Country } from "../../ts/interfaces/Country.interface";
import useAutocomplete from "../../hooks/useAutocomplete";
import classes from "./ui.module.css";
interface Props {
@ljaviertovar
ljaviertovar / Autocomplete.tsx
Last active October 3, 2022 01:15
Autocomplete search component
import { Card, Col, Input, Row, Text, User } from "@nextui-org/react"
import { useEffect, useRef, useState } from "react"
import { Country } from "../../ts/interfaces/Country.interface"
import classes from "./ui.module.css"
interface Props {
data: Country[];
}
@ljaviertovar
ljaviertovar / config-dummy-reausableModals.ts
Last active June 16, 2022 19:35
A reusable Modal in React ts
import {
ModalConfigDummy,
ModalPositionX,
ModalPositionY,
} from "./ts/interfaces/modal.interface";
export const INITIAL_CONFIG: ModalConfigDummy = {
modal1: {
title: "Modal Header 1",
showHeader: true,
@ljaviertovar
ljaviertovar / PortalModal-reusableModals.tsx
Last active June 16, 2022 19:29
A reusable Modal in React ts
import { useState, useLayoutEffect } from "react";
import { createPortal } from "react-dom";
interface Props {
children: JSX.Element;
wrapperId: string;
}
const PortalModal = ({ children, wrapperId }: Props) => {
const [portalElement, setPortalElement] =
@ljaviertovar
ljaviertovar / App-reusableModals.tsx
Last active June 16, 2022 19:26
A reusable Modal in React ts
import { FC, useState } from "react";
import Header from "./components/layout/Header";
import { Buttons, Modal } from "./components/modals";
import { ThemeProvider } from "styled-components";
import { lightTheme, darkTheme, GlobalStyles } from "./styles/theme";
import * as S from "./components/modals/styles";
import { INITIAL_CONFIG } from "./config-dummy";
@ljaviertovar
ljaviertovar / Modal-reusableModals.tsx
Last active June 15, 2022 14:06
A reusable Modal in React ts
import { useCallback, useEffect, useRef } from "react"
import PortalModal from "./PortalModal"
import useOnClickOutside from "../../hooks/useOnClickOutside"
import { ModalConfig } from "../../ts/interfaces/modal.interface"
import * as S from "./styles"
import "../../styles/modal.css"
@ljaviertovar
ljaviertovar / cleanupWebSockets.js
Last active May 18, 2022 15:07
Cleaning up Web Sockets functions in React
useEffect(() => {
const ws = new WebSocket(url, protocols)
// do what you want with the socket
ws.onmessage = (event) => {
setValue(JSON.parse(event.data));
};
// cleanup the web socket when component unmout
return () => ws.close()
}, [])
@ljaviertovar
ljaviertovar / cleanupIntervals.js
Last active May 18, 2022 04:34
Cleaning up Intervals functions in React
useEffect(() => {
let intervalId = setInterval(() => {
// do something like update the state
}, 1000);
// cleanup the timer when component unmout
return () => clearInterval(interval);
}, []);
@ljaviertovar
ljaviertovar / cleanupTimeout.js
Last active May 18, 2022 01:23
Cleaning up Timeout functions in React
useEffect(() => {
let timerId = setTimeout(() => {
// do something
timerId = null;
}, 3000);
// cleanup the timmer when component unmout
return () => clearTimeout(timerId);
}, []);