0403 (express static, api reg etc., react routing etc.) Flashcards
What is the purpose of the Express Static middleware?
it serves static files
What does express.static() return?
it returns a middlewear
What are several examples of static files?
images, index.html, main.js, styles.css? files u just get and deliver as is.
extra:
non static files: sound files, files u need server for, files you import (you are running it)
What is a good way to serve application images using Express?
put them in an images folder inside (public)?
What does fetch() return?
A Promise that resolves to a Response object.
What is the default request method used by fetch()?
GET
How do you specify the request method (GET, POST, etc.) when calling fetch?
How does fetch report errors?
fetch is a promise so it can do that
How can useEffect be used to load data for a component?
with fetch?
What browser function can be used to make HTTP requests to a server in React?
fetch
How do you use useEffect to load component data just once when the component mounts?
by using empty brackets (an empty array) as a dependency?, e.g. [ ]
How do you use useEffect to load component data every time the data key changes?
pass that key as a dependency in the array, [ ]
In a large-scale production app, what are some better alternatives for loading and managing backend data?
Some good open source packages are React Query and Vercel SWR work.
errors:
500s are?
400s are?
which is client/server?
500 server
400 client
What is the purpose of React “context”?
To be able to pass data from a (distant) parent to many children instead of manually passing props indiviaually
.
What values can be stored in context?
static values and also values which can be updated
How do you create context and make it available to the components?
import “crateContext”:
import { createContext } from ‘react’;
create it in a file and export it:
export const LevelContext = createContext(1);
How do you access the context values?
by wrapping children which are going to use it and passing to them thru a parent (close or distant)
When would you use context? (in addition to the best answer: “rarely”)
when u don’t want to set data manually on many children
react-custom-hooks
react-custom-hooks
What is a React custom hook?
When are custom hooks useful? When are they not required?
writing less with ordinary hooks.
when another hook is NOT called.
What is the syntax (or naming convention) for writing a custom hook?
How do you call a custom hook?
When do custom hooks execute?
What are unit tests?
A way to check if code (usually a function) meets certain conditions and does what is intended. It’s like console logging in that you can check your work, but much more sophisticated.
Officiallty:
A unit test is a way of testing a unit - the smallest piece of code that can be logically isolated in a system. In most programming languages, that is a function, a subroutine, a method or property.
source:
https://smartbear.com/learn/automated-testing/what-is-unit-testing/
Why is it important to write unit tests?
To test code, to refactor code, debug
What code should be tested with a unit test? What code is not well suited for unit tests?
a unit. small piece of code.
What is Jest? What are some other popular JavaScript unit testing frameworks?
A unit testing framework for JS.
Others are Jest, Mocha, Jasmine, AVA
In JavaScript, when is scope determined?
What allows JavaScript functions to “remember” values from their surroundings?
What values does a closure contain?
all that are in scope
When is a closure created?
when a function is created
How can you tell if a function will be created as a closure?
In React, what is one important case where you need to know if a closure was created?
if u have a function that is a dependency in ur useEffect, u need to know if it’s a closure.
you will get a lint error if it’s a closure. to fix this, wrap the function and use a callback.
what is a closure
a function which has a reference to a function and varialbes outside of it that it uses in the function