Skip to content

Instantly share code, notes, and snippets.

View Alimjanov-Ibragim's full-sized avatar
🎯
Focusing

Ibragim Alimjanov-Ibragim

🎯
Focusing
View GitHub Profile
@ali-sabry
ali-sabry / useMediaQuery.js
Created March 26, 2023 13:34
This custom hook will allow me to easily check if the current screen size matches a specific media query (e.g., desktop or mobile). By using this hook, I can conditionally render components based on the screen size and optimize my app for different devices.
//=== Custom Hook useMediaQuery
import { useState, useEffect } from 'react';
export const useMediaQuery = (query) => {
const [matches, setMatches] = useState(false);
useEffect(() => {
const mediaQuery = window.matchMedia(query);
/* window.matchMedia() match if the media query you passed
is match with current media query and return boolean value
@ali-sabry
ali-sabry / useDebounce.js
Created March 25, 2023 20:07
This hook will allow me to debounce any function that I want to run after a certain delay. For example, if I have an input field that triggers a search function on every keystroke, this hook will prevent unnecessary API calls by delaying the search function until the user stops typing for a certain period.
import { useState, useEffect } from 'react';
export const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const timer = setTimeout(() => {
setDebouncedValue(value);
}, delay);

How to force a page to reload from server at browser launch with JavaScript

I spent weeks finding a way to refresh a page or a tab from the server once a user starts or restarts the browser. I was very surprised to find nothing about this topic.

Let's dig into the context, tries and the solution I came up with.

Context

Typically, once a user opens a browser and gets to a page, it directly provides the page in cache without refreshing any data from the server. Obviously, this only applies if the user is not in a private navigation.

In this case, as a user, I always need to open the browser and then manually reload the page to get the last updated data. What? Am I too lazy? Sure I am a developer!

@KamaMakh
KamaMakh / UA parser
Created June 24, 2022 05:22
Detect Browser, Engine, OS, CPU, and Device
export const isSafari = () => {
const ua = window.navigator.userAgent.toLowerCase();
return (
ua.includes('safari') &&
!ua.includes('crios') &&
!ua.includes('fxios') &&
!ua.includes('android')
);
};
@loonywizard
loonywizard / react-use-throttle-hook-usage.tsx
Last active September 5, 2023 08:19
Example of usage of React useThrottle hook
import React, { useEffect, useState } from 'react'
import { useThrottle } from './useThrottle'
export default function App() {
const [value, setValue] = useState('hello')
const throttledValue = useThrottle(value)
useEffect(() => console.log(`throttledValue changed: ${throttledValue}`), [
throttledValue,
@loonywizard
loonywizard / react-use-throttle-hook.ts
Created March 30, 2021 14:14
React useThrottle hook
import { useEffect, useRef, useState } from 'react'
function useThrottle<T>(value: T, interval = 500): T {
const [throttledValue, setThrottledValue] = useState<T>(value)
const lastExecuted = useRef<number>(Date.now())
useEffect(() => {
if (Date.now() >= lastExecuted.current + interval) {
lastExecuted.current = Date.now()
setThrottledValue(value)
@KamaMakh
KamaMakh / base64Methods.js
Created December 10, 2020 10:35
Methods for base64
{
toBase64: file => new Promise((resolve, reject) => {
if (file) {
const reader = new FileReader()
reader.readAsDataURL(file.raw)
reader.onload = () => resolve(reader.result)
reader.onerror = error => reject(error)
} else {
reject()
}
@KamaMakh
KamaMakh / axiosTemp.js
Last active December 10, 2020 11:18
axios template
let axios = require("axios")
let axiosInstance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
transformRequest: [(data, headers) => {
headers["Accept-Language"] = "en";
if (/* token */) {
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
/**
* @param {string} str
@rickyhaswifi
rickyhaswifi / netlify.toml
Last active August 10, 2023 00:48
React Router Netlify - Fix for not found page
#https://stackoverflow.com/questions/58065603/netlify-renders-404-on-page-refresh-using-react-and-react-router
[[redirects]]
from = "/*"
to = "/index.html"
status = 200