mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-06 12:57:44 +00:00
docs: finish mutable update code samples
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
||||
|
||||
# Compiled output
|
||||
/dist
|
||||
/tmp
|
||||
/out-tsc
|
||||
/bazel-out
|
||||
|
||||
# Node
|
||||
/node_modules
|
||||
npm-debug.log
|
||||
yarn-error.log
|
||||
|
||||
# IDEs and editors
|
||||
.idea/
|
||||
.project
|
||||
.classpath
|
||||
.c9/
|
||||
*.launch
|
||||
.settings/
|
||||
*.sublime-workspace
|
||||
|
||||
# Visual Studio Code
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
.history/*
|
||||
|
||||
# Miscellaneous
|
||||
/.angular/cache
|
||||
.sass-cache/
|
||||
/connect.lock
|
||||
/coverage
|
||||
/libpeerconnection.log
|
||||
testem.log
|
||||
/typings
|
||||
|
||||
# System files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
@@ -0,0 +1,71 @@
|
||||
{
|
||||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
||||
"version": 1,
|
||||
"newProjectRoot": "projects",
|
||||
"projects": {
|
||||
"DemoApp": {
|
||||
"projectType": "application",
|
||||
"schematics": {},
|
||||
"root": "",
|
||||
"sourceRoot": "src",
|
||||
"prefix": "app",
|
||||
"architect": {
|
||||
"build": {
|
||||
"builder": "@angular-devkit/build-angular:browser",
|
||||
"options": {
|
||||
"outputPath": "dist/demo-app",
|
||||
"index": "src/index.html",
|
||||
"main": "src/main.ts",
|
||||
"polyfills": ["zone.js"],
|
||||
"tsConfig": "tsconfig.app.json",
|
||||
"scripts": []
|
||||
},
|
||||
"configurations": {
|
||||
"production": {
|
||||
"budgets": [
|
||||
{
|
||||
"type": "initial",
|
||||
"maximumWarning": "500kb",
|
||||
"maximumError": "1mb"
|
||||
},
|
||||
{
|
||||
"type": "anyComponentStyle",
|
||||
"maximumWarning": "2kb",
|
||||
"maximumError": "4kb"
|
||||
}
|
||||
],
|
||||
"outputHashing": "all"
|
||||
},
|
||||
"development": {
|
||||
"buildOptimizer": false,
|
||||
"optimization": false,
|
||||
"vendorChunk": true,
|
||||
"extractLicenses": false,
|
||||
"sourceMap": true,
|
||||
"namedChunks": true
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "production"
|
||||
},
|
||||
"serve": {
|
||||
"builder": "@angular-devkit/build-angular:dev-server",
|
||||
"configurations": {
|
||||
"production": {
|
||||
"browserTarget": "DemoApp:build:production"
|
||||
},
|
||||
"development": {
|
||||
"browserTarget": "DemoApp:build:development"
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "development"
|
||||
},
|
||||
"extract-i18n": {
|
||||
"builder": "@angular-devkit/build-angular:extract-i18n",
|
||||
"options": {
|
||||
"browserTarget": "DemoApp:build"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12815
content/blog/ffg-fundamentals-side-effects/ffg-fundamentals-angular-mutable-update-title-43/package-lock.json
generated
Normal file
12815
content/blog/ffg-fundamentals-side-effects/ffg-fundamentals-angular-mutable-update-title-43/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "@ffg-fundamentals/angular-mutable-update-title-43",
|
||||
"version": "0.0.0",
|
||||
"scripts": {
|
||||
"ng": "ng",
|
||||
"dev": "ng serve",
|
||||
"start": "ng serve",
|
||||
"build": "ng build",
|
||||
"watch": "ng build --watch --configuration development"
|
||||
},
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@angular/animations": "^16.2.0",
|
||||
"@angular/common": "^16.2.0",
|
||||
"@angular/compiler": "^16.2.0",
|
||||
"@angular/core": "^16.2.0",
|
||||
"@angular/forms": "^16.2.0",
|
||||
"@angular/platform-browser": "^16.2.0",
|
||||
"@angular/platform-browser-dynamic": "^16.2.0",
|
||||
"@angular/router": "^16.2.0",
|
||||
"rxjs": "~7.8.0",
|
||||
"tslib": "^2.3.0",
|
||||
"zone.js": "~0.13.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@angular-devkit/build-angular": "^16.2.10",
|
||||
"@angular/cli": "^16.2.10",
|
||||
"@angular/compiler-cli": "^16.2.0",
|
||||
"typescript": "~5.1.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Angular Mutable Update Title - Example #43 - FFG Fundamentals</title>
|
||||
</head>
|
||||
<body>
|
||||
<app-root></app-root>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,65 @@
|
||||
import "zone.js/dist/zone";
|
||||
import { bootstrapApplication } from "@angular/platform-browser";
|
||||
|
||||
import { Component, NgZone, OnDestroy, inject } from "@angular/core";
|
||||
import { NgIf } from "@angular/common";
|
||||
|
||||
@Component({
|
||||
selector: "title-changer",
|
||||
standalone: true,
|
||||
template: `
|
||||
<div>
|
||||
<button (click)="updateTitle('Movies')">Movies</button>
|
||||
<button (click)="updateTitle('Music')">Music</button>
|
||||
<button (click)="updateTitle('Documents')">Documents</button>
|
||||
<p>{{ title }}</p>
|
||||
</div>
|
||||
`,
|
||||
})
|
||||
export class TitleChangerComponent implements OnDestroy {
|
||||
title = "Movies";
|
||||
|
||||
timeoutExpire: any = null;
|
||||
|
||||
// This is using "Dependency Injection" (chapter 11)
|
||||
// To access Angular's internals and expose them to you
|
||||
ngZone = inject(NgZone);
|
||||
|
||||
updateTitle(val: string) {
|
||||
clearTimeout(this.timeoutExpire);
|
||||
// Do not run this in runOutsideAngular, otherwise `title` will never update
|
||||
const expire = setTimeout(() => {
|
||||
this.title = val;
|
||||
document.title = val;
|
||||
}, 5000);
|
||||
|
||||
this.ngZone.runOutsideAngular(() => {
|
||||
this.timeoutExpire = expire;
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy() {
|
||||
clearTimeout(this.timeoutExpire);
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: "app-root",
|
||||
standalone: true,
|
||||
imports: [NgIf, TitleChangerComponent],
|
||||
template: `
|
||||
<div>
|
||||
<button (click)="toggle()">Toggle title changer</button>
|
||||
<title-changer *ngIf="show" />
|
||||
</div>
|
||||
`,
|
||||
})
|
||||
export class AppComponent {
|
||||
show = true;
|
||||
|
||||
toggle() {
|
||||
this.show = !this.show;
|
||||
}
|
||||
}
|
||||
|
||||
bootstrapApplication(AppComponent);
|
||||
@@ -0,0 +1,10 @@
|
||||
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./out-tsc/app",
|
||||
"types": []
|
||||
},
|
||||
"files": ["src/main.ts"],
|
||||
"include": ["src/**/*.d.ts"]
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
||||
{
|
||||
"compileOnSave": false,
|
||||
"compilerOptions": {
|
||||
"baseUrl": "./",
|
||||
"outDir": "./dist/out-tsc",
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"noImplicitOverride": true,
|
||||
"noPropertyAccessFromIndexSignature": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"sourceMap": true,
|
||||
"declaration": false,
|
||||
"downlevelIteration": true,
|
||||
"experimentalDecorators": true,
|
||||
"moduleResolution": "node",
|
||||
"importHelpers": true,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"useDefineForClassFields": false,
|
||||
"lib": ["ES2022", "dom"]
|
||||
},
|
||||
"angularCompilerOptions": {
|
||||
"enableI18nLegacyMessageIdFormat": false,
|
||||
"strictInjectionParameters": true,
|
||||
"strictInputAccessModifiers": true,
|
||||
"strictTemplates": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>React Mutable Update Title - Example #43a - FFG Fundamentals</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.jsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
2056
content/blog/ffg-fundamentals-side-effects/ffg-fundamentals-react-mutable-update-title-43/package-lock.json
generated
Normal file
2056
content/blog/ffg-fundamentals-side-effects/ffg-fundamentals-react-mutable-update-title-43/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@ffg-fundamentals/react-mutable-update-title-43",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-react": "^4.0.4",
|
||||
"vite": "^4.4.9"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { useState, useRef, useEffect } from "react";
|
||||
|
||||
const TitleChanger = () => {
|
||||
const [title, setTitle] = useState("Movies");
|
||||
|
||||
const timeoutExpire = useRef(null);
|
||||
|
||||
function updateTitle(val) {
|
||||
timeoutExpire.current = setTimeout(() => {
|
||||
setTitle(val);
|
||||
document.title = val;
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
return () => clearTimeout(timeoutExpire.current);
|
||||
}, [timeoutExpire]);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<button onClick={() => updateTitle("Movies")}>Movies</button>
|
||||
<button onClick={() => updateTitle("Music")}>Music</button>
|
||||
<button onClick={() => updateTitle("Documents")}>Documents</button>
|
||||
<p>{title}</p>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const App = () => {
|
||||
const [show, setShow] = useState(true);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<button onClick={() => setShow(!show)}>Toggle title changer</button>
|
||||
{show && <TitleChanger />}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
createRoot(document.getElementById("root")).render(<App />);
|
||||
@@ -0,0 +1,6 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>React useRef Timestamp - Example #43b - FFG Fundamentals</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.jsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
2056
content/blog/ffg-fundamentals-side-effects/ffg-fundamentals-react-use-ref-timestamp-43/package-lock.json
generated
Normal file
2056
content/blog/ffg-fundamentals-side-effects/ffg-fundamentals-react-use-ref-timestamp-43/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@ffg-fundamentals/react-use-ref-timestamp-43",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-react": "^4.0.4",
|
||||
"vite": "^4.4.9"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { useRef, useEffect } from "react";
|
||||
|
||||
const Comp = () => {
|
||||
const ref = useRef();
|
||||
|
||||
useEffect(() => {
|
||||
ref.current = Date.now();
|
||||
});
|
||||
|
||||
// First render won't have `ref.current` set
|
||||
return <p>The current timestamp is: {ref.current}</p>;
|
||||
};
|
||||
|
||||
createRoot(document.getElementById("root")).render(<Comp />);
|
||||
@@ -0,0 +1,6 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>React useRef Timestamp Rerender - Example #43c - FFG Fundamentals</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.jsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@ffg-fundamentals/react-use-ref-timestamp-rerender-43",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-react": "^4.0.4",
|
||||
"vite": "^4.4.9"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { useRef, useState, useEffect } from "react";
|
||||
|
||||
const Comp = () => {
|
||||
// Set initial value for first render
|
||||
const ref = useRef(Date.now());
|
||||
|
||||
// We're not using the `_` value, just the `set` method to force a re-render
|
||||
const [_, setForceRenderNum] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
ref.current = Date.now();
|
||||
});
|
||||
|
||||
return (
|
||||
<div>
|
||||
{/* This value will only update when you press "check timestamp" */}
|
||||
<p>The current timestamp is: {ref.current}</p>
|
||||
<button onClick={() => setForceRenderNum((v) => v + 1)}>
|
||||
Check timestamp
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
createRoot(document.getElementById("root")).render(<Comp />);
|
||||
@@ -0,0 +1,6 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Vue Mutable Update Title - Example #43 - FFG Fundamentals</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
1099
content/blog/ffg-fundamentals-side-effects/ffg-fundamentals-vue-mutable-update-title-43/package-lock.json
generated
Normal file
1099
content/blog/ffg-fundamentals-side-effects/ffg-fundamentals-vue-mutable-update-title-43/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "@ffg-fundamentals/vue-mutable-update-title-43",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"vue": "^3.3.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-vue": "^4.2.3",
|
||||
"vite": "^4.4.9"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<!-- App.vue -->
|
||||
<script setup>
|
||||
import { ref } from "vue";
|
||||
import TitleChanger from "./TitleChanger.vue";
|
||||
|
||||
const show = ref(true);
|
||||
|
||||
function toggle() {
|
||||
show.value = !show.value;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<button @click="toggle()">Toggle title changer</button>
|
||||
<TitleChanger v-if="show" />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,26 @@
|
||||
<!-- TitleChanger.vue -->
|
||||
<script setup>
|
||||
import { watch, ref, onUnmounted } from "vue";
|
||||
const title = ref("Movies");
|
||||
|
||||
let timeoutExpire = null;
|
||||
|
||||
function updateTitle(val) {
|
||||
clearTimeout(timeoutExpire);
|
||||
timeoutExpire = setTimeout(() => {
|
||||
title.value = val;
|
||||
document.title = val;
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
onUnmounted(() => clearTimeout(timeoutExpire));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<button @click="updateTitle('Movies')">Movies</button>
|
||||
<button @click="updateTitle('Music')">Music</button>
|
||||
<button @click="updateTitle('Documents')">Documents</button>
|
||||
<p>{{ title }}</p>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,5 @@
|
||||
// main.js
|
||||
import { createApp } from "vue";
|
||||
import App from "./App.vue";
|
||||
|
||||
createApp(App).mount("#root");
|
||||
@@ -0,0 +1,6 @@
|
||||
import { defineConfig } from "vite";
|
||||
import vue from "@vitejs/plugin-vue";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [vue()],
|
||||
});
|
||||
Reference in New Issue
Block a user