Skip to content

Instantly share code, notes, and snippets.

@harshaktg
harshaktg / App.js
Created November 19, 2021 17:30
SWR implementation
import useSWR from "swr";
import "./App.css";
function App() {
// Fetcher function
const fetcher = (...args) => fetch(...args).then((res) => res.json());
// SWR implementation
const { data, error } = useSWR(
"https://jsonplaceholder.typicode.com/posts/1",
@harshaktg
harshaktg / index.js
Created November 15, 2021 23:00
React 18 auto batching
import { useState, useLayoutEffect } from "react";
import * as ReactDOM from "react-dom";
function App() {
const [count, setCount] = useState(0);
const [flag, setFlag] = useState(false);
function handleClick() {
console.log("=== click ===");
fetchSomething().then(() => {
@harshaktg
harshaktg / App.js
Created June 19, 2021 18:35
AWS Amplify App page
import "./App.css";
import Home from "./Home";
function App() {
return (
<div className="App">
<header className="App-header">
<Home />
</header>
</div>
@harshaktg
harshaktg / Home.js
Created June 19, 2021 18:28
AWS Amplify Home page
import Amplify from "aws-amplify";
import awsExports from "./aws-exports";
import { AmplifySignOut, withAuthenticator } from "@aws-amplify/ui-react";
Amplify.configure(awsExports);
const Home = (props) => {
return (
<div>
<AmplifySignOut />
<h1>This is your logged in page.</h1>
@harshaktg
harshaktg / App.js
Created June 2, 2021 18:37
useAxios - Step 4
import useAxios from './useAxios';
const App = () => {
const { response, error, loading } = useAxios({
method: 'POST',
url: '/posts',
headers: {
accept: '*/*',
},
data: {
@harshaktg
harshaktg / useAxios.js
Last active June 2, 2021 18:40
useAxios - Step 4 custom hook file
import { useState, useEffect } from 'react';
import axios from 'axios';
axios.defaults.baseURL = 'https://jsonplaceholder.typicode.com';
const useAxios = (params) => {
const [response, setResponse] = useState(null);
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
@harshaktg
harshaktg / App.js
Created June 2, 2021 18:29
useAxios - Step 3
import useAxios from './useAxios';
const App = () => {
const { response, error, loading } = useAxios();
return (
<div className="app">
{loading ? (
<div>Loading...</div>
) : (
<div>
@harshaktg
harshaktg / useAxios.js
Created June 2, 2021 18:28
useAxios - Step 3 custom hook file
import { useState, useEffect } from 'react';
import axios from 'axios';
axios.defaults.baseURL = 'https://jsonplaceholder.typicode.com';
const useAxios = () => {
const [response, setResponse] = useState(null);
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
@harshaktg
harshaktg / App.js
Created June 2, 2021 18:23
useAxios - Step 2
import { useState, useEffect } from 'react';
import axios from 'axios';
axios.defaults.baseURL = 'https://jsonplaceholder.typicode.com';
const App = () => {
const [response, setResponse] = useState(null);
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
@harshaktg
harshaktg / App.js
Last active June 2, 2021 18:22
useAxios - Step 1
import { useEffect } from 'react';
import axios from 'axios';
axios.defaults.baseURL = 'https://jsonplaceholder.typicode.com';
const App = () => {
const fetchData = async () => {
try {
const res = await axios.get('/posts');
console.log(res.data);