Skip to content

Instantly share code, notes, and snippets.

View mhaecal's full-sized avatar

Muhaecal D. Khatami mhaecal

View GitHub Profile
@mhaecal
mhaecal / App.js
Created February 15, 2022 02:33
React Hook Form Validation with Error Message
import React from 'react'
import { useForm } from 'react-hook-form'
import { ErrorMessage } from '@hookform/error-message'
function App() {
// prettier-ignore
const { register, handleSubmit, formState: { errors } } = useForm()
const onSubmit = ({ username, password }) => {
alert(`Username: ${username}, Password: ${password}`)
@mhaecal
mhaecal / App.js
Last active February 28, 2022 04:11
React Query with Pagination Example
import React from 'react'
import Character from './components/Character'
import { QueryClientProvider, QueryClient } from 'react-query'
const queryClient = new QueryClient()
function App() {
return (
<div className="App">
<QueryClientProvider client={queryClient}>
@mhaecal
mhaecal / tsconfig.json
Created February 17, 2022 04:17
Typescript Configuration for React
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
@mhaecal
mhaecal / App.tsx
Created February 19, 2022 04:32
RTK Query with TypeScript
import React, { useState } from 'react'
import {
useGetPokemonByIdQuery,
useGetPokemonByNameQuery,
} from './services/pokemon'
function App() {
const [id, setId] = useState<number>(5)
const [name, setName] = useState<string>('bulbasaur')
@mhaecal
mhaecal / App.tsx
Last active February 20, 2022 04:47
React Router 6
import React from 'react'
import { Outlet } from 'react-router-dom'
export default function App() {
return <Outlet />
}
@mhaecal
mhaecal / index.js
Created June 7, 2022 14:16
Sort JavaScript Array of Objects Numerically / Alphabetically
items.sort(function(a, b) {
return a.id - b.id || a.name.localeCompare(b.name);
});
@mhaecal
mhaecal / index.md
Created June 13, 2022 15:34
Checkbox w/ Custom Background Image (Tailwind)
@mhaecal
mhaecal / index.js
Created June 18, 2022 08:49
onClick Child Component to Fire a function in Parent Component (onClink Props React)
const { useState } = React;
function PageComponent() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1)
}
return (
<div className="App">
@mhaecal
mhaecal / middleware.ts
Created July 19, 2022 08:25
Protected Routes Middleware NextJS 12
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(req: NextRequest) {
// get cookie token
const hasToken = req.cookies.get('token')
// protected routes (admin routes)
if (req.nextUrl.pathname.startsWith('/admin')) {
if (hasToken) {
@mhaecal
mhaecal / _app.tsx
Last active July 23, 2022 09:05
Nextjs Multiple Layouts
import '../styles/globals.css'
import type { AppProps } from 'next/app'
import type { NextPage } from 'next'
import type { ReactElement, ReactNode } from 'react'
export type NextPageWithLayout = NextPage & {
getLayout?: (page: ReactElement) => ReactNode
}
type AppPropsWithLayout = AppProps & {