await fetch(url)
: returns aResponse
object.await
waits for the method to complete.
await response.text()
: returns thehtml
string.DOMParser().parseFromString(postHtml, "text/html")
: returns aDocument
object, which is a complete DOM tree from the HTML string;DOMParser
is a built-in browser API.postDoc.getElementById("content")
: returns anElement
with thecontent
id name.content.querySelector(".post-title a")
: fetches the firstElement
with thepost-title
class name, and returns the firstElement
the first<a>
(anchor) tag..
for classes,#
for IDs (IDs should be unique within a page)
Posts tagged "web":
15 Aug 2024
Web learning in practice
This post records the basic web development knowledge I have learned in practice.
1. Basic html structure
2. Tags
<a>
: contain links; have following attributes:target="_blank"
: open the link in a new tab.title="Go to the link"
: the tooltip message, i.e., the floating message when a user hovers over.
<span>
: don’t add line breaks before or after it.<hr>
: horizontal rule.
3. Attributes
id="home"
:- allow specific styling of the element, e.g.,
#home ul {...}
only styles theul
in the block with the same id.
- allow specific styling of the element, e.g.,
4. Javascript
4.1. Fetch from an HTML URL
4.2. Modify a DOM
content.querySelector(".taglist").remove()
: removes the element from the DOM, i.e., it modifiescontent
.
4.3. Syntax
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
: add a backslash to any special character in the query;$&
is a special pattern used in Javascript’sreplacement
method.
23 Jun 2024
Weblab notes: React route
This is a personal note for the web.lab lectures.
1. Router
- use the Reach Reach Router library
URL -> Router -> render different components
2. Link
- relative:
<Link to="newpage">Click me</Link>
- absolute:
<Link to="/newpage">Click me</Link>
3. Workshop 3
3.1. Structure

3.2. States
name | states |
---|---|
Feed | stories : a list of stories |
Card | comments : a list of comments for a story id |
3.3. Props
index | props |
---|---|
1 | a function to update stories |
2 | all attributes in a story |
3 | the attributes used to display a story |
4 | a story id; a list of comments under the story; a function to update comments |
5 | all attributes in a comment |
6 | a comment id; the function to update comments |
3.4. Why passing down the update function in props 1, 4, 6?
- To share the parent states, i.e.,
stories
andcomments
to child component. Since the post action happens in the child component, we need a way to automatically update the states to see new contents immediately.
23 Jun 2024
Weblab notes: React hooks
This is a personal note for the web.lab lectures.
1. What is a React hook
- Special functions to access parts of the component lifestyle.
- e.g.,
useState
1.1. useState
is not enough
- The output of
console.log
is[]
instead of["me"]
because setting a state is async! - To do something immediately after a state is changed, use
useEffect
hook!
1.2. useEffect
runs after specific variable change
useEffect(myFunction, [var1, var2])
callsmyFunction
everytime whenvar1
orvar2
changesuseEffect(myFunction, []])
calls only once when the component is rendered for the first time (on mount)useEffect(myFunction)
calls at every render
2. React hook patterns
2.1. Fetch and send data
2.2. Conditional rendering
2.3. Render an array of Data
key
is a special prop in React; it is used identify which item has changed efficiently
3. Example: Stopwatch
4. DOM and component mounting
- DOM (Document Object Model): a programming interface for web documents; represents the structure of a document, e.g., HTML, as a tree of objects, where each object corresponds to a part of the document; it dynamically updates the document contents
- React is a framework that manipulates DOM
- A React component is unmounted when:
- conditional rendering
- routing; navigating from one route to another
- its parent component is unmounted