Skip to content

Instantly share code, notes, and snippets.

View richleach's full-sized avatar

Rich Leach richleach

View GitHub Profile
@richleach
richleach / layout.tsx
Last active November 30, 2023 01:11
JSM layout.tsx with children and TAILWIND CSS TO FULLY CENTER CONTENT IN MIDDLE OF PAGE
import React from "react";
const Layout = ({ children }: { children: React.ReactNode }) => {
return (
<main className="flex min-h-screen w-full items-center justify-center">
{children}
</main>
);
};
@richleach
richleach / parseJsInsideJSX.js
Created April 6, 2022 23:00
Run and print results of JS inside the actual return statement of a Next.js React component. Parse on-demand, inside the JSX
//in this sample, assume an axios get request returned a number between 1 and 5 which was stored in state (stAffection).
//instead of parsing it out above the return block and possibly creating another variable, this is a way to do it "inline"
return (
<>
//...HTML formatting stuff, maybe some output, mapping, etc.
<h3 className="m-2"><span className="font-semibold">Affection level: </span>
{(() => {
switch (stAffection) {
case 5: return " Most affectionate";
//array of objects
const myItems = [{"name": "eggs", "price": 1},{"name": "coffee", "price": 9.99}, {"name": "rich", "price": 4.04}]
const sortByName = myItems.sort(function(a,b) {
let nameA = a.name.toUpperCase()
let nameB = b.name.toUpperCase()
if(nameA < nameB) {
return -1
}
if(nameA > nameB){
@richleach
richleach / LoopingOverJSON.js
Created January 5, 2022 19:38
Loop over JSON and sort results by one of the JSON name elements
//array of objects
const myItems = [{"name": "eggs", "price": 1},{"name": "coffee", "price": 9.99}, {"name": "rich", "price": 4.04}]
//If you want to sort myItems by price.
const revised = myItems.sort((function(a,b){
return a.price - b.price
}))
console.log(revised);
@richleach
richleach / Counter.test.js
Created June 10, 2021 14:45
REACT TESTING LIBRARY Test file
import React from 'react'
import Counter from '../Counter'
import { render, fireEvent } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'
test('Header renders with correct text', () => {
const { getByTestId } = render(<Counter />)
const headerEl = getByTestId("header")
expect(headerEl.textContent).toBe("My Counter")
@richleach
richleach / Counter.js
Created June 10, 2021 14:43
REACT TESTING LIBRARY - component used to test against
import React, { useState } from 'react'
import "../Counter/counter.css"
function Counter() {
const [counterValue, setCounterValue] = useState(0)
const [inputValue, setInputValue] = useState(1)
const addToCounter = () => {
setCounterValue(counterValue + inputValue)
}
@richleach
richleach / postingMaterialUIFormContentToJSONServer.js
Created April 21, 2021 23:45
Some more Material UI code, forms and how to handle a form submission. More custom styles, using the POST method inside of fetch()
import React, { useState } from 'react'
import Typography from '@material-ui/core/Typography'
import Button from '@material-ui/core/Button'
import Container from '@material-ui/core/Container'
import KeyboardArrowRightOutlinedIcon from '@material-ui/icons/KeyboardArrowRightOutlined';
import { makeStyles, TextField, Radio, RadioGroup, FormControlLabel, FormLabel, FormControl } from '@material-ui/core'
import { useHistory } from 'react-router-dom'
const useStyles = makeStyles({
field: {
@richleach
richleach / NoteCard.js
Created April 21, 2021 23:39
Material UI goodness: How to make custom CSS styles looking at the props of the component and using the makeStyles(). Some basic MUI components in here, doc supported.
import React from 'react'
import Card from '@material-ui/core/Card'
import CardHeader from '@material-ui/core/CardHeader'
import CardContent from '@material-ui/core/CardContent'
import { IconButton, makeStyles, Typography } from '@material-ui/core'
import { DeleteOutlined } from '@material-ui/icons'
import Avatar from '@material-ui/core/Avatar'
import { blue, green, pink, yellow } from '@material-ui/core/colors'
const useStyles = makeStyles({
@richleach
richleach / Notes.js
Created April 21, 2021 23:35
This file shows how to use the JSON Server, how to format GET requests inside of a useEffect call and how to format a DELETE request (it uses array.filter() to filter out the recently deleted note).) Also, a little bit of Material UI love in here too.
//this file calls the JSON Server, passes each returned note as a prop to the NoteCard component
import React, {useState} from 'react'
import { useEffect } from 'react'
import Grid from '@material-ui/core/Grid'
import { Container } from '@material-ui/core'
import NoteCard from '../components/NoteCard'
export default function Notes() {
const [notes, setNotes] = useState([])
@richleach
richleach / db.json
Created April 21, 2021 23:31
JSON Server handy to use when your backend/API/endpoints aren't available but maybe you know the data/object structure. You have to npm install it, then run it to watch a particular file (endpoint).
//Using JSON Server
//Install JSON Server locally
npm install -g json-server
//Run JSON Server
json-server --watch data/db.json --port 8000
//which means http://localhost:8000/notes will return the 4 JS objects below.
//Create a file like below named "db.json". All of the objects below are part of the "notes" endpoint
{