Skip to content

Instantly share code, notes, and snippets.

View osamajavaid's full-sized avatar
🌾
available

osamajavaid

🌾
available
View GitHub Profile
@osamajavaid
osamajavaid / useForm.jsx
Created September 24, 2023 16:16
React useForm hook: Creates a stateful value from the fields in a form.
const useForm = initialValues => {
const [values, setValues] = React.useState(initialValues);
return [
values,
e => {
setValues({
...values,
[e.target.name]: e.target.value
});
@osamajavaid
osamajavaid / useLocalStorage.jsx
Created September 24, 2023 16:08
React useLocalStorage hook: Creates a stateful value that is persisted to localStorage, and a function to update it.
const useLocalStorage = (keyName, defaultValue) => {
const [storedValue, setStoredValue] = React.useState(() => {
try {
const value = window.localStorage.getItem(keyName);
if (value) {
return JSON.parse(value);
} else {
window.localStorage.setItem(keyName, JSON.stringify(defaultValue));
return defaultValue;
@osamajavaid
osamajavaid / LazyLoadImage.jsx
Created September 24, 2023 15:58
Renders an image that supports lazy loading.
const LazyLoadImage = ({
alt,
src,
className,
loadInitially = false,
observerOptions = { root: null, rootMargin: '200px 0px' },
...props
}) => {
const observerRef = React.useRef(null);
const imgRef = React.useRef(null);
@osamajavaid
osamajavaid / countries-list.jsx
Created September 15, 2023 19:43
All Countries list, that can be used in select input
//////////////////////////////////
// all coutries lists
//////////////////////////////////
export const countryOptions = [
{ value: "Afghanistan", label: "Afghanistan" },
{ value: "Åland Islands", label: "Åland Islands" },
{ value: "Albania", label: "Albania" },
{ value: "Algeria", label: "Algeria" },
{ value: "American Samoa", label: "American Samoa" },
@osamajavaid
osamajavaid / Invalidate-multiple-queries.jsx
Created September 15, 2023 06:41
React Query: Invalidate multiple queries
import { useQuery, useQueryClient } from 'react-query';
// Inside your component or function...
const queryClient = useQueryClient();
// Example: Invalidate a single query
queryClient.invalidateQueries('yourQueryKey');
// Example: Invalidate multiple queries
@osamajavaid
osamajavaid / vite.config.js
Created September 15, 2023 05:43
This file contain aliasing for reactjs folder structure and svgr package for importing svg icons as react components
import react from '@vitejs/plugin-react';
import svgr from "vite-plugin-svgr";
export default {
plugins: [react(), svgr()],
server: {
port: 8081,
},
resolve: {
alias: [