Skip to content

Instantly share code, notes, and snippets.

@lejonmanen
lejonmanen / fire.js
Last active April 15, 2024 18:41
Firestore CRUD from static frontend 2024
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getFirestore, collection, addDoc, getDocs, getDoc, doc, updateDoc, deleteDoc } from 'firebase/firestore/lite'
// Get this from Firebase Console
import { firebaseConfig } from "./firebaseConfig.js";
// Initialize Firebase and get database
const app = initializeApp(firebaseConfig);
const db = getFirestore(app)
@lejonmanen
lejonmanen / App.jsx
Last active February 29, 2024 16:57
Conditional rendering exercise
/*
The Budget component uses "conditional rendering" to do
different things based on its props. What will each line
in the App component render?
*/
const App = () => (
<main>
<Budget value={5500} />
<Budget value={240} />
@lejonmanen
lejonmanen / Aaa.jsx
Last active February 29, 2024 16:37
React components
// A component is a function that returns JSX
const Aaa = () => (
<div>
You can do any JSX in here
</div>
)
// Remember to export the component
// Most people have one component per file and use export default
export default Aaa
@lejonmanen
lejonmanen / example.js
Created November 24, 2023 13:38
Using fetch with try+catch
// Simple request
async function sendRequest(url) {
try {
const response = await fetch(url)
if( response.status !== 200 ) {
console.log('Request wasn't successful. Code: ' + response.status)
return null
}
const data = await response.json()
return data // success
@lejonmanen
lejonmanen / example.js
Last active November 24, 2023 13:08
Sync vs async
// Option 1: use a promise directly
let data;
function itTakesTime1() {
return new Promise((resolve, reject) => {
/* do something that takes time and produces a value */
resolve(value)
})
}
@lejonmanen
lejonmanen / index.js
Last active October 29, 2023 16:38
Using Node.js async readline
import { getQuestion } from './readline.js'
const [question, close] = getQuestion()
let input = await question('Please input a number')
console.log(`The number is: ${input}.`)
close()
@lejonmanen
lejonmanen / Ajax.jsx
Last active April 13, 2023 10:13
Send AJAX request in React component
import { useState, useEffect } from 'react'
// This function can be in a separate file
async function getData(setData) {
const url = 'API url here'
const response = await fetch(url)
const data = await response.json()
setData(data)
}
@lejonmanen
lejonmanen / verses.js
Last active April 2, 2023 20:33
Some verses from a Shakespeare sonnet
// https://www.gutenberg.org/files/100/100-0.txt
// The Project Gutenberg eBook of The Complete Works of William Shakespeare, by William Shakespeare
// (But this file only has the first 12)
const verses = ` 1
From fairest creatures we desire increase,
That thereby beauty’s rose might never die,
But as the riper should by time decease,
@lejonmanen
lejonmanen / CountAtom.js
Created April 2, 2023 18:55
React Recoil example
import { atom } from 'recoil'
// The key can be any unique value
// Default is the initial value
const countState = atom({
key: 'countState',
default: ''
});
export { countState }
@lejonmanen
lejonmanen / Ancestor.jsx
Last active April 12, 2023 12:33
React context example
import { useState } from 'react'
import { CounterContext } from './CounterContext.js'
/* The ancestor is the component that owns this particular piece of data. The ancestor is the only component that knows how to modify it. Therefore, if the descendants should be able to modify the value, we must send the set function as well.
If the descendants only need to read the value, we replace the object "countPackage" with the value "count".
*/
const Ancestor = () => {
// Initialize using default data
const [count, setCount] = useState(10)