Skip to content

Instantly share code, notes, and snippets.

View simicd's full-sized avatar

Dejan Simic simicd

View GitHub Profile
[tool. Ruff]
line-length = 88
target-version = "py310"
# See https://beta.ruff.rs/docs/rules/
select = [
"F", # Pyflakes
"E", # pycodestyle (Errors)
"W", # pycodestyle (Warnings)
"I", # isort
{
// Editor & Debuger
"editor.suggestSelection": "first",
"editor.minimap.renderCharacters": false, // Use text representation in minimap on the right side
"editor.minimap.showSlider": "always", // Highlight visible code area in minimap on right side
"editor.copyWithSyntaxHighlighting": true, // CTRL + C will also copy the code highlighting
"editor.cursorBlinking": "solid", // Change cursor blinking style
"editor.multiCursorModifier": "ctrlCmd", // Use CTRL + Mouse click to place multiple cursors and write simulatnously
"editor.guides.bracketPairs": true, // Colorize vertical line indicating bracket range on the left hand side
"editor.bracketPairColorization.enabled": true, // Colorize pairs of brackets
import { useState, useEffect, useCallback } from "react";
export const useFetch = ({ url, init, processData }) => {
// Response state
const [data, setData] = useState();
// Turn objects into strings for useCallback & useEffect dependencies
const [stringifiedUrl, stringifiedInit] = [JSON.stringify(url), JSON.stringify(init)];
// If no processing function is passed just cast the object to type T
import { useState, useEffect, useCallback } from "react";
interface RequestProps<T> {
url: RequestInfo;
init?: RequestInit;
processData?: (data: any) => T;
}
export const useFetch = <T>({ url, init, processData }: RequestProps<T>) => {
// Response state
import { useState, useEffect, useCallback } from "react";
// ...
export const useFetch = <T>({ url, init, processData }: RequestProps<T>) => {
// ...
// Define asynchronous function
const fetchApi = async () => {
// ...
};
// DogImageWithButton.tsx
import React, { FC } from "react";
import { useFetch } from "./useFetch";
type DogImageType = { message: string; status: string };
export const DogImageWithButton: FC = () => {
/** Fetch image on button click */
const { data, fetchApi } = useFetch<DogImageType>({
url: "https://dog.ceo/api/breed/beagle/images/random",
// DogImageWithButton.tsx
import React, { FC } from "react";
import { useFetch } from "./useFetch";
type DogImageType = { message: string; status: string };
export const DogImage: FC = () => {
/** Fetch image on button click */
const getImage = () => {
const data = useFetch<DogImageType>({
/**
* Paste into Browser for smooth scrolling from top to bottom
* @see https://css-tricks.com/smooth-scrolling-for-screencasts/
*/
const scrollElement = (element, scrollPosition, duration) => {
// useful while testing to re-run it a bunch.
// element.removeAttribute("style");
const style = element.style;
import { useState, useEffect, useCallback } from "react";
export const useFetch = ({ url, init, processData }) => {
// Response state
const [data, setData] = useState();
// Turn objects into strings for useCallback & useEffect dependencies
const [stringifiedUrl, stringifiedInit] = [JSON.stringify(url), JSON.stringify(init)];
// If no processing function is passed just return the data
import { useState, useEffect, useCallback } from "react";
interface RequestProps<T> {
url: RequestInfo;
init?: RequestInit;
processData?: (data: any) => T;
}
export const useFetch = <T>({ url, init, processData }: RequestProps<T>) => {
// Response state