Skip to content

Instantly share code, notes, and snippets.

@faahmad
faahmad / ci-react-firebase.yaml
Created January 13, 2021 01:44
GitHub Actions Workflow for a React x Firebase app (includes Cloud Functions, Preview Channels)
# https://twitter.com/farazamiruddin
# My GitHub Actions workflow for pull requests. Feel free to use this as inspiration.
name: CI React Firebase
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
@faahmad
faahmad / oceanic-next-overrides.json
Last active September 9, 2021 13:46
Oceanic Next theme oveerrides
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"name": "Comment",
"scope": "comment",
"settings": {
"fontStyle": "italic"
}
},
{
@faahmad
faahmad / opionated-react-04-state-colocation-good.tsx
Created April 27, 2020 14:40
Opinionated React: Use State Colocation - better :)
import * as React from "react";
export const PersonForm: React.FC = () => {
return (
<form>
<label htmlFor="firstName">First name</label>
<PersonInput name="firstName" />
<label htmlFor="lastName">Last name</label>
<PersonInput name="lastName" />
</form>
@faahmad
faahmad / opinionated-react-04-state-colocation-bad.tsx
Created April 27, 2020 14:38
Opinionated React: Use State Colocation - don't do this
interface PersonFormState {
firstName: string;
lastName: string;
}
interface PersonFormAction {
type: "SET_VALUE";
payload: { [name: string]: string };
}
function personFormReducer(state: PersonFormState, action: PersonFormAction) {
switch (action.type) {
@faahmad
faahmad / opionated-react-3-4.tsx
Created March 6, 2020 16:28
Opinionated React 3: State Management
interface MovieListState {
isLoading: boolean;
movies: Movie[];
error: string;
}
type MoveListAction =
| { type: "fetching" }
| { type: "success"; payload: Movie[] }
| { type: "error"; error: Error };
const initialMovieListState: MovieListState = {
@faahmad
faahmad / opionated-react-3-3.tsx
Created March 6, 2020 16:27
Opinionated React 3: State Management
export const MovieList: React.FC = () => {
const [isLoading, setIsLoading] = React.useState<boolean>(true);
const [movies, setMovies] = React.useState<Movie[]>([]);
const [error, setError] = React.useState<string>("");
const handleFetchMovies = () => {
setIsLoading(true); // 😢
setError(""); // 😢
return MovieService.fetchInitialMovies()
.then(initialMovies => {
setMovies(initialMovies);
@faahmad
faahmad / opionated-react-3-2.tsx
Created March 6, 2020 16:25
Opinionated React 3: State Management
const MovieList: React.FC = () => {
const [isLoading, setIsLoading] = React.useState<boolean>(true)
const [movies, setMovies] = React.useState<Movie[]>([])
React.useEffect(() => {
MovieService
.fetchInitialMovies()
.then(initialMovies => setMovies(initialMovies))
.then(() => setIsLoading(false))
}, [])
if (isLoading) {
@faahmad
faahmad / opionated-react-3-1.tsx
Created March 6, 2020 16:23
Opinionated React 3: State Management
const MovieList: React.FC = () => {
const [movies, setMovies] = React.useState<Movie[]>([])
React.useEffect(() => {
MovieService
.fetchInitialMovies()
.then(initialMovies => setMovies(initialMovies))
}, [])
return (
<ul>
{movies.map(movie => <li key={movie.id}>{movie.title}</li>}
@faahmad
faahmad / opinionated-react-2-code-structure.tsx
Last active February 23, 2020 20:27
Component File Structure
import * as React from "react";
import { gql } from "apollo-boost";
import { useQuery } from "@apollo/react-hooks";
import { Movie } from "../types/Movie";
const MOVIE_LIST_QUERY = gql`
query MovieListQuery($movieListId: ID!) {
movieList(movieListId: $movieListId) {
id
name
@faahmad
faahmad / AppRoutes.tsx
Last active February 16, 2020 14:56
Opinionated Guide to React - AppRoutes.tsx
import React from "react";
import { Switch, BrowserRouter, Route } from "react-router-dom";
import { useAuthContext } from "./contexts/AuthContext";
import { Navbar } from "./components/Navbar";
import { LandingPage } from "./pages/LandingPage";
import { DashboardPage } from "./pages/DashboardPage";
export const AppRoutes: React.FC = () => {
const authAccount = useAuthContext();