Year #2, Week #5 đť ď¸đş
New stuff we learned this week: đ¤
Acronyms, Acronyms, Acronyms!
IIFE
- a javacript function can be immediately invoked if you use parentheses around the function, and then another set of parentheses to invoke:
(function() {
console.log("Hello World!");
})();
- this technique is called an Immediately Invoked Function Expression
(IIFE), and used to be used often to control scope before we had
block-scoped variables with
let
andconst
.
ASI
- Javascript doesnât require you to add a semicolon at the end of the line, like
C
does. Brendan Eich was trying to be nice to newbies or something. But if you omit semicolons, then javacript inserts them for you, which is called Automatic Semicolon Insertion. Usually this doesnât cause a problem, but there are a few times where it can cause unexpected behavior. - for instance, the following javacript (if it wasnât fixed automatically by prettier) would result in a weird outcome:
function foobar() {
return
{
foo: 'bar'
}
}
- after ASI does itâs job, the above code would be changed to:
function foobar() {
return; // đ¨ WHOOPS!
{
foo: "bar",
};
}
- this is why multi-line JSX expressions that are returned from functions must be grouped using parens:
return (
<div>
<h1>Hello World!</h1>
</div>
);
SPA
- some websites arenât really sites theyâre more like applications disguised as a website. For these types of âsitesâ, we donât really need multiple pages, as the user clicks around and does stuff, weâll just change what they see with javascript. Sites like these are called SPAs which stands for Single Page Applications.
- SPAs usually send down just a small HTML shell that loads the javascript necessary to run the app. But things like search engine bots and programs to help disabled users often donât run javascript, which can be a problem.
- plus, if there arenât unique URLs for each state of the app, you canât refresh your page and get back to the same spot, or share a URL with your buddy.
SEO
- SEO stands for Search Engine Optimization â which means crafting your website so that computers from search engine companies (like Google and DuckDuckGo) can figure out from your HTML what your site is all about, and recommend it to people when they search.
- without a decent amout of extra work, SPAs can be pretty bad for SEO, so for things where SEO really matters, developers usually donât build SPAs.
SSG
- a while back, people realized they wanted the convenience of writing websites with reusable components with something like React, but they also wanted the performance and SEO benefits of building sites with a bunch of plain old HTML files, so they came up with the idea of Static Site Generators, or SSG. A SSG basically loops over a bunch of page files (or maybe some other data source) and converts your code into a folder full of plain old HTML files. These files can be uploaded to a web host. If you change something on your site, you can just re-generate your files, which takes only seconds.
- examples of SSGs include: Gatsby, Nextjs, Hugo, Eleventy, and MANY others.
- in the React world, SSGs are made possible by the fact that
ReactDOM
has a method:renderToSTring
which, instead of creating a virtual DOM, just hands you back a plain string of HTML, like this:
const html = ReactDOM.renderToString(<h1>Hi!</h1>);
console.log(html); // "<h1>Hi!</h1>"
Useful Links:
Homework Plan
- 1 day (non-app) flashcard homework
- 1 day touch typing practice
- 1 day Git review
- 4 days Execute Program homework
- 1 or 2 days Flashcard App homework
(non-app) Flashcard homework
- slowly and carefully review all of your flashcards
- re-review the ones you got wrong
- add 2 more cards in the
javascript
category:ASI
IIFE
- add 3 cards to a new
web
category:SPA
SEO
SSG
Git Review
- starting with week 26, slowly and carefully read all of the
git
sections of âNew Stuffâ from the main 4 weeks of last year where we were learning git. Here are the links:
Flashcard App homework
- slowly and carefully review all of the âNew Stuffâ above ^.
- DO NOT start this homework until you have done the âGit Reviewâ homework
- Get on to GitLab, and merge your own MR from last weeks homework.
- connect to the flashcard app again with vscode
- switch over to your master branch, then type a command that will update the
master
branch so that it pulls in the commits you just merged into master on GitLab. (If you canât figure this out, check the git review links above, and remember, gitlab is a remote for your local working git repo) - type another command now to view the commits on your master branch. you should be able to verify that your merged in commits are now in master for you locally. donât do the next step until youâve confirmed that.
- type a command to delete the branch you were working on for last weeks homework.
- create a new branch from master called
create-spa
and switch to that branch - start up your storybook server and forward your port
- modify the
Button
component, making two changes. Make sure typescript is happy and that storybook still renders the button after you make these changes:- first, the current
Button
component accepts astring
prop calledlabel
which is the text that appears on the button. This means you have to do stuff like:<Button label="Click me">
. Change it so that it uses the specialchildren
prop. Once youâre done, you should use the component like this:<Button>Click Me</Button>
. Youâll have to change yourButton.stories.tsx
file as well. If youâre confused by this step, rewatch the children prop video I made. - second make the
Button
component accept a prop calledonClick
which is a function that takes zero arguments and returnsany
. The Button component should wire this up to the native html<button>
elementsonClick
handler so that whatever function you pass it gets run when a user clicks the button.
- first, the current
- next, modify the
<Chrome>
component so that it renders two buttons, one that says<< Prev
and one that saysNext >>
. Make it so that these buttons are displayed on the left and right of where theCard
component is (or will be) displayed. REQUIRED: use one of the special values forjustify-content
to make this look evenly spaced and centered. - next, get a literal pencil and paper, and design a data structure to hold
the information about your flashcards. A data structure is just a fancy
word for a combination of javascript primitives like strings, numbers,
booleans, arrays, and objects.
- hint, remember your flash cards have (at least) a âcategoryâ, a value for the âfrontâ of the card, and a value for the âbackâ
- hint 2, remember, you have more than one card
- hint 3, there is no true right answer for this, there are several good ways to design this
- requirement: design at least 2 data structures that would work, then weigh the pros and cons of each, and pick which one you think will work best in your App.
- once youâve got your data structure designed, create a
card-data.ts
file in your./src
dir which will export your cards as the default export. It might look something like this:
// these are all two simple, none of these would work
// just demonstrating that you would export
// whatever your structure is
export default "my data structure is a string";
// or...
export default ["my", "structure", "is", "array"];
// or...
export default { my: "structure", is: "an object" };
- populate your
card-data.ts
file with the info from at least 3 real flashcards. - Now, stop and read the rest of the steps/requirements before doing anything else.
- To finish the assignment, youâre going to actually get your web app (not
storybook) working (at least sort of). This means youâll have to re-write the
placeholder
App.tsx
component to render yourChrome
component. - when itâs working:
- it should start by displaying the category and âfrontâ of the first card you entered in step 14.
- if you click the card component, it should switch to showing the âbackâ or answer for that card
- if you click the âPrevâ or âNextâ buttons, you should move back and forth between which card is showing
- you donât need to worry about going past the end (or going before the beginning) of your list of cards. If someone mashes on your âNextâ button too many times, itâs OK if it just errors, weâll fix that later.
- the bottom part of the Chrome component should show the real number of cards, and your position.
- now that you know the requirements, step away from the computer and get out a
pencil and paper again (I mean it â itâs important to learn to think through
problems clearly without writing code first). This time, on pencil and paper,
figure out what STATE you will need to make this thing work. Hint: you
will need more than one piece of state â meaning, more than piece of data
created and managed by
useState
. What will be the types of the pieces of state? How will you use that state to pass the right props to the components youâve built? - Once you think you have designed your state correctly, re-read the requirements on step 17 and ask yourself how you will accomplish each part using the combination of your custom data structure and the state youâve designed. Does it cover all the needs and features? If not, go back to step 18 and try another design. Itâs OK if you do this several times, designing the state of an App is hard, and you often donât get it right the first time.
- Before you go and try to build this, here are a few more hints and
requirements:
- commit early and often, that way if you mess something up, you can always
throw away your work with
git reset
and go back to an earlier point in time when you were happy with your work. - remember BABY STEPS â if you try to do this all at once, you will certainly struggle. Instead, ask yourself âwhatâs the smallest piece of this I could try to get workingâ, and then tackle that. When you succeed, commit and repeat.
- your
App
component should hold all the state for your app, and be in charge of passing down props into your other components. This includes passing down functions to do things when buttons (or the card) is clicked. These functions should be arrow functions wrapping some call to update your state. If this feels confusing, review your own code from the âReact audiosâ homework, you did the same thing there. - youâre probably going to need to alter your
Chrome
component so that it takes a couple more props â functions to handle what happens when the Prev and Next button are pressed. - youâre also probably going to need to alter the
Card
component so that it accepts a function to do something when clicked. This is what will âflipâ the card, showing the back. - hint: when you âflipâ a card to show the back, the Card component doesnât really need to know about that, you can just pass in a different value for the prop that displays in the middle of the card. Itâs good to keep your components as dumb as possible.
- youâll want to spin up your dev server with
npm start
while youâre working on this, fixing errors and taking baby steps. restart the dev server if something seems wonky. - read your error messages carefully and pay attention to red squiggles in vscode, they will usually lead you to the problem if you take a moment to digest what they are saying to you.
- commit early and often, that way if you mess something up, you can always
throw away your work with
- When youâve got it done, go back to your
*.stories.tsx
files, and fix any errors in there. (these would be caused if you added new props while you were working on the app, and didnât pass those props in your story files. For functions, you can just pass dummy, empty functions, like<Button onClick={() => {}}>
) - then, I want to you increase the number of cards from 3 to 15 real cards.
- modify line
8
of yourpackage.json
file so that when you run thenpm run build
it builds to a folder calledwww/flashcards
instead ofwww/parcel-boilerplate
- commit any remaining uncommited work
- then, build your site by running
npm run build
. - push up a MR from your
create-spa
branch and slack me both the web url of the app, and the MR url in the #homework channel. - Kiah Credit: ⨠fix the error that would result from trying to click âPrevâ or âNextâ too many times. There are several ways to do this, decide what seems best and implement your design.