Skip to content

Instantly share code, notes, and snippets.

View sanusart's full-sized avatar

Sasha Khamkov sanusart

View GitHub Profile
@sanusart
sanusart / f1
Created January 24, 2025 07:09
desc
f1
dsdf
@sanusart
sanusart / use-is-online.tsx
Created January 23, 2025 13:58
is online hook #react #hooks
import { useEffect, useState } from 'react';
export const useIsOnline = () => {
const [online, setOnline] = useState<boolean>(true);
const checkOnlineStatus = () => setOnline(navigator.onLine);
useEffect(() => {
window.addEventListener('online', checkOnlineStatus);
window.addEventListener('offline', checkOnlineStatus);
@sanusart
sanusart / readme.md
Last active February 24, 2025 09:12
use intersection observer hook #react #hooks

Usage example

const LazyListItem = () => {
 const [isInView, ref] = useIntersectionObserver<HTMLDivElement>();

 return (
   <div ref={ref}>
     {isInView ? (
@sanusart
sanusart / Table.js
Created July 18, 2020 14:31
React [{}] to table #reactjs
const Table = ({data }) => {
if(data.length === 0) return null;
const rows = Object.keys(data[1]);
return (
<table>
<thead>
<tr>
{rows.map(key => (
<th>{key}</th>
@sanusart
sanusart / meta-middlware.js
Last active July 17, 2020 08:55
Redux meta-middlware to add some meta to every action #redux
import { PACKAGE_NAME } from 'src/constants';
const metaMiddleware = ({ getState }) => next => (action) => {
const store = getState();
const user = store[PACKAGE_NAME].user;
const transport = store[PACKAGE_NAME].transport;
next({
...action,
meta: {
@sanusart
sanusart / useListIterate.js
Last active March 7, 2020 09:13
useListIterate hook - iterates over array of items with predefined timing #react #hooks
import React, { useState, useEffect } from 'react';
export default function useListIterate(items, interval = 3000) {
const [item, setItem] = useState(items[0]);
useEffect(() => {
let index = 0;
const timer = setInterval(() => {
setItem(items[index]);
@sanusart
sanusart / git-files-changes.sh
Created December 9, 2019 19:42
Git match changes in commit #bash #shell
#!/usr/bin/env bash
set -e
# Usage:
#
# bash ./scripts/git-files COMMIT_HASH
#
# Usage in script:
#
@sanusart
sanusart / Portal.jsx
Created October 25, 2019 20:52
React portal component #reactjs #react #portal #hooks
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';
const portalTarget = document.getElementById('portal');
const Portal = ({ children }) => {
const element = document.createElement('div');
useEffect(() => {
portalTarget.appendChild(element);
@sanusart
sanusart / git-files-match.sh
Last active December 8, 2019 22:29
Check if file with certain extension exists in a commit #nodejs #git
#!/bin/bash
# Usage:
# ./scripts/git-files d304b82
#
# Or in script:
#
# if [ `sh ./scripts/git-files d304b82` -gt 0 ];
# then
# echo "matches";