COMP 426 Midterm 2

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/97

encourage image

There's no tags or description

Looks like no tags are added yet.

98 Terms

1
New cards

Browser

application used to review and display information from the web

2
New cards

When developing websites, it is important to check compatibility and make sure sites work well on all the most prominent browsers

true

3
New cards

Rendering Engine

responsible for rendering the DOM on the browser screen

4
New cards

networking service

responsible for making network requests to servers

5
New cards

storage

other browser features + connections for device hardware

6
New cards

javascript runtime

functionality to run javascript code

7
New cards

Web APIs

serve as the interface between the JS Runtime and browser features

  • implemented by each individual browser according to the same standard, so each webAPI should be functional regardless of browser


8
New cards

Which objects or functions have we used built into JS/TS that allowed us to interact with browser features (such as the rendering engine or networking)?

fetch, document, console, setTimeout etc

9
New cards

when the call stack is empty, it is time to run waiting tasks

true

10
New cards

event loop

responsible for moving this task onto the call stack

  • constantly checks if the call stack is empty and if so, it will move a waiting task into the call stack


11
New cards

microtask queue

has a higher priority than the task queue; executes immodestly after the current script before the next macrotask

  • promise handlers, callbacks, and continuations are added to the microtask queue

  • .then(), .catch(), .finally(), .async/await


12
New cards

tasks queue

executes one task per event loop turn; only one task is processed at a time per event loop iteration

  • setTimeout(), setInterval(), setImmediate, DOM events, fetch()


13
New cards

React

library for developing user interfaces and managing state

  • make user interfaces composable → react components

  • better manage state and UI updates when an app’s state changes


14
New cards

react components

describe a UI element in react

  • defined using functions that returns JSX

  • JSX enables HTML and JS/TS to be combined into one file, combining UI and functionality specifications into one file


15
New cards

JSX

a syntax extension to JS that enables the user to write markup similar to HTML in JS/TS files

16
New cards

React components are defined as functions

True

17
New cards

React components return JSX

True

18
New cards

The input of component functions are props (properties) like attributes in HTML (these are type script objects)

true

19
New cards

composability

other components can be nested inside of components via JSX

20
New cards

If all of our components are functions, how does our page load React components to begin with?


<p></p>
21
New cards

component hierarchy

knowt flashcard image
22
New cards

reduction of files

with react we can reduce the number of necessary files

23
New cards

typescript tools

static typing

24
New cards

we installed react to better handle creating user interfaces

true

25
New cards

TSX (view + styling + functionality)

allow us to eliminate writing CSS files, making the code more organized and easier to maintain

26
New cards

Tailwind

CSS framework that provides built in classes to apply styles to elements directly in markup (HTML/TSX)

  • removes unnecessary abstractions of classes and maintaining styles

  • allows the developer to easily see which styles are applied to elements in-line

  • reduce dependencies on extra CSS files

  • allows UI components to be specified into one single (.tsx) file

  • has built in classes


27
New cards

Wouldn’t tailwind be less efficient to use, since it would add a lot of “bloat” (unused CSS) to our app?

when our app build, Tailwind will remove all unused classes, leaving only the CSS that is used

  • this reduces our bundle size, decreasing our app’s load time


28
New cards

state

refers to the current “configuration” of our app’s data and its presentation

  • an immutable reference to the state’s value


29
New cards

model (comp 301)

stores the application data

30
New cards

view (comp 301)

shows the UI

31
New cards

controller (comp 301)

  1. called on to manipulate the data stored in the model

  2. updates the view to reflect the new state of the model


32
New cards

what would the state of our app (pixel art marker) include?

app data and the UI

33
New cards

UI and State Relationship

  1. actions on the UI changes the app’s state

  2. state changes then updates the UI

Ex: the user clicks on the eraser tool button, changing the active tool stored in the state → the state (active tool) changes, so then the UI updates to show that the eraser is selected



34
New cards

react utilized the architectural model of the ui and state not the MVC

True

35
New cards

JSX features

  • extensible: can define your own components

  • composable

  • programmable: component defined as a function runs whenever rendered; can program this function to dynamically and conditionally render as needed

  • reflects the current state of the application


36
New cards

your code in react should almost never be dealing directly with the DOM

True

  • your code should be nothing but components

  • components produce JSX

  • react translates this to updates on the DOM


37
New cards

Where is the state in react? If our application is now just a dynamically rendered JSX, then where is the actual application state?

in the components

38
New cards

react state hook

react uses the useState() hook to maintain state

  • const [state, setState] = useState(initialValue);


39
New cards

setState

a function used to update the state’s value

40
New cards

useState(initalValue)

sets up a state variable with an initial value

41
New cards

how react “reacts” to state changes

  1. setState() is called

  2. triggers the react’s re-rendering process

  3. react finds all DOM elements that rely on/use that state and re-renders them

  4. react commits this change by mutating the DOM

Trigger → render → commit

  • you MUST use setState() to trigger a re-render (counter+=1 does not work)

    • counter+=1 causes the UI to not update on the state change, causing the state and UI to become out of sync


42
New cards

In React, it is common to refer to the view as a function of its state

what does the function mean?

the user interface is a direct, predictable output generated from the current state date

<p><span>the user interface is a direct, predictable output generated from the current state date</span></p>
43
New cards

What text will display on the screening the paragraph after the user presses the button?

Jane Doe

<p>Jane Doe</p>
44
New cards

If multiple state changes occur in one action, only one re-render occurs

True

  • these changes are queued in order and run once the action finished, otherwise, a re-render could start mid-action


45
New cards

What happens if you added a state for the word and when you load your data, you set the state to be that word?

every time you change a state, you rerun the body of the component function

46
New cards

side effects

any action that a component takes that is not rendering the UI (or updating state)

  • do not want to trigger a re-render


47
New cards

How do you handle side effects

the useEffect() hook

  • useEffect can handle all three triggers of side effects

  • Run on every render, Run on first render, and Run on state change


<p>the useEffect() hook </p><ul><li><p>useEffect can handle all three triggers of side effects</p></li><li><p>Run on every render, Run on first render, and Run on state change</p></li></ul><p></p>
48
New cards

Without useEffect, every re-render would add a new event handler

True

49
New cards

The useEffect() hook also can return a cleanup function that runs before the effect reruns or when the component unmounts. This is crucial to preventing memory leaks!!

True

<p>True</p>
50
New cards

memoization

preserving (caching) the result of an “expensive operation” every re-render and only recalculating it when needed

51
New cards

React is a library, not a standalone framework (a tool inside the tool box)

True

52
New cards

React does not have a built in router (no built in way to display specific pages)

True
- React knows how to build things on a webpage, but it doesn't automatically know which page to show when someone visits a specific URL

53
New cards

What happens when we try to load another page other than the home page?

we would have to load the new .html page then load the corresponding css and js

54
New cards

Multi-page Application (MPA)

every time we navigate to a new page, the browser requests a new HTML page “blowing" up”/deleting everything and starting entirely from scratch

55
New cards

Router

library/tool that hijacks this traditional process

  • instead of using <a> links connected to new pages, routers usually provide their own link component that should be used instead

  • when a router link is pressed, the router clears the DOM and replaces it with a new page component

  • The router then changes the active URL in the browser to reflect a page change

  • Routers use browser APIs to override default browser behavior, preventing the browser from trying to load a new resource when the URL changes


The router handles page changes, and renders using the JS loaded initially because all components are part of the JS bundle. It just switches the page that it is showing


56
New cards

Single page application (SPA)

the app logs once on the client, then routing actions shuffle the active component on the screen

  • the app does not restart every time we try to load a new page

  • when we add a router, our app becomes this type of application


57
New cards

What is an example of a single page application?

an angular framework project

58
New cards

What ways are SPAs worse than MPAs?

search engine optimization, initial load times, memory consumption, security complexity

59
New cards

How do new users find new websites?

search engines

60
New cards

In an SPA app, if crawlers cannot run JS, they will never see more than just the home page of the site because there is only index.html

True

61
New cards

Search engine optimization (SEO)

how do we engineer our site in a way that makes our site more accessible to more people on search engines

62
New cards

SEO vs MPA vs SPA

  • For search engines optimization (SEO): multi-page application (MPAs) > single page application (SPAs)

  • FOR UX: SPAs > MPAs

  • For Interactivity: SPAs > MPAs

  • For Speed: SPAs > MPAs


63
New cards

Client-side rendering (CSR)

SPAs have HTML rendered entirely on the client using the downloaded React code in the JS bundle (HTML files are an empty shell!)

64
New cards

When Next.js projects build and deploy, server code is generated as well

True

65
New cards

What occurs after the client requests the website’s home page?

Next.js tries to generate the HTML file for this page (using React) on the server first

  • this HTML file should look Iike the page, it is not just a blank shell, the the DOM gets updated and shows the right page on the browser


66
New cards

Server-side rendering (SSR)

the initial HTML is generated on the server not the client (Next.js)

  • affects the first load when we enter the app (afterwards, we treat the app like an SPA)


67
New cards

Hydration

reconcile the existing page and sprinkle in functionality (JavaScript)

68
New cards

SSR (server side rendering) vs SPA (single page application)

  • SPA: the loading ends and the crawler works with what it sees

  • SSR: pre-rendered page with Next.js.

    • the page is not interactive, but it usually contains enough content for the crawler to index this page well


69
New cards

[slug]

matches a single path component

70
New cards

[… slug] and [[…slug]]

matches all remaining path components and double bracket version matches optionally

<p>matches all remaining path components and double bracket version matches optionally</p>
71
New cards

page.tsx

request result is rendered react component

  • often any related subcomponents in same directory

  • wrapped in layouts nested from root


72
New cards

Route.ts

request is handled as an API endpoint

73
New cards

useState and useEffect are for client components

true

74
New cards

When to fetch?

  • As a client component: initial load has empty list, request to API made by

    • Show the page first, get the data later

  • As a server component: request to API made by server and list rendered to HTML server-side on initial load

    • Get the data first, then show the page

  • Pros and Cons:

    • Pro Client: interactive time is shorter because client-side components come up quicker

    • Pro Server: web crawling because you are not running any JS


75
New cards

Anatomy of a client-side component

  • “use client”

  • path parameters accessed with useParams() hook

  • fetch via useEffect

    • requires then handlers

    • results in updated state → triggers rerendering


76
New cards

Client data fetching vs Server data fetching

  • Pros Client data fetching: has a faster FCP (first contentful paint), but a better UX

  • Cons client data fetching: should conditionally render while loading, may need to render differently on error, lots of performance issues, race conditions between re-renders

  • Pros Server data fetching: has a worse FCP (first contentful paint), but better SEO (search engines optimization)

    • SEO only matters if data incorporated into the page is not specific to the user or user’s input

  • Note: In most cases, we prefer to stick with client data fetching unless the SEO boost is very important - we prefer to always prioritize FCP


77
New cards

first contentful paint (FCP)

measures the time from when a user visits a page to when any part of the page’s content is rendered on the screen

78
New cards

server side component

  • allowed to be async

    • means that we can just await the params prop

    • similarly, can await fetch() and .json() of the response when fetching data server-side


79
New cards

ReactQuery

  • React Query is a smart assistant for API data.

    • QueryClientProvider = Gives your app access to the assistant.

    • queryKey = Tells the assistant which information you want.

    • queryFn = Tells the assistant how to get it.

    • data = The information it found.

    • isLoading = Whether the initial request is still loading.

    • error = What went wrong.


80
New cards

We can use localStorage.get() and localStorage.set() to store data in the browser that our website can access between sessions

true

81
New cards

Why is storing application data in the frontend bad?

  • reloading the app will reset state because the app reloads

  • data stored in one browser is not accessible in another browser


82
New cards

database

structured collection of data that can be used to store information

83
New cards

relational database

connect related data through relationships

84
New cards

CRUD operations

C: create

R: read

U: update

D: delete

85
New cards

we can use a database URL to connect to the database

true

86
New cards

tables and what they contain

data in databases are stored in this

  • columns: values for each record are stored here and have a specific data type

  • data: stored in rows (records)

  • primary key: uniquely identify records

  • foreign keys: connect records across tables


87
New cards

foreign key

columns that store the ID of records in another column to link data together

88
New cards

one-to-many relationship

one user can have many todo items, but a todo item can only have one user

89
New cards

Foreign key column are placed on the “many” side of the one-to-many relationship because columns cannot hold lists

true

  • because of this limitation, we need an association table


90
New cards

association table

a third table to handle many-to-many relationships


91
New cards

composite primary key

primary key is set to two columns so items are uniquely represented by the blue (pk1, pk2)

92
New cards

schematic diagram

a visual blueprint that maps out the structure of a relational database

  • we specify the PK(*), FK(←), data types and relationships


<p>a visual blueprint that maps out the structure of a relational database</p><ul><li><p>we specify the PK(*), FK(←), data types and relationships</p></li></ul><p></p>
93
New cards

SQL (Structured Query Language)

built for querying and working with data in a relational database

  • we can run SQL commands on a database from within our TypeScript projects, but we would have to define this SQL commands in raw strings

  • the result we will get from SQL is also in a string format (not easily parsable by TypeScript)


94
New cards

call stack

where the code runs

95
New cards

task queue

where callbacks wait

96
New cards

event loop

makes sure waiting callbacks do not interrupt code that is already executing

97
New cards

If a mounted React component re-renders because its state changes, calling useState(10) again inside the component function resets that state to 10.

False. React preserves state across re-renders of the same mounted component. The initial value is used when state is initialized, not on every re-render.

98
New cards

What does response.json() return?

response.json() is asynchronous and returns a Promise that fulfills with the parsed JavaScript data.