Week 3: React
Goals
- Learn rationale behind React
- Understand why we use JS bundlers and become familiar with
vite, our bundler of choice- Learn Typescript rationale and basics
- Create a basic React app
React Basics
- Read https://react.dev/
- Read https://react.dev/learn
Now, check your knowledge:
- What is React and why would you use it over plain HTML/CSS/JavaScript?
- What is JSX? How is it different from HTML?
- What is the difference between props and state in React? When would you use each?
- What is a component in React?
- Explain what the
useStatehook does. What do the two values it returns represent?- How do you handle events in React (e.g., button clicks)? How is this different from handling events in plain JavaScript?
- Why do we need to use
keyprops when rendering lists in React? What happens if you don’t include them?
JS Bundlers
- Read https://dev.to/sayanide/the-what-why-and-how-of-javascript-bundlers-4po9
- Read https://vite.dev/, which is our bundler of choice when possible.
- Read https://vite.dev/guide/ and run the commands there.
- Read https://vite.dev/guide/philosophy
- Read https://vite.dev/guide/why
- Using vite, create a new app called
dstl-onboarding-vite.- Select
Reactas framework - Select
Typescript + SWCas the variant - Select
Nofor “Use rolldown-vite (Experimental)?:” - Select
Yesfor “Install with npm and start now?”
- Select
- Vite should start a dev server. Open the page in your browser
- Test hot module reloading by clicking the button a few times, then editing some of the text in
App.tsx. You should see the page update live without reloading your button count!
Now, check your knowledge:
- What is a JavaScript bundler and why do we need one? What problems do bundlers solve that can’t be handled with plain HTML
<script>tags?- List all the files/folders that get changed when you run
npm install @tanstack/react-query.- What is more appropriate, and why:
npm install prettier? Ornpm install -D prettier?- What is the difference between a development build and a production build? Why does Vite optimize differently for each?
- What is Hot Module Replacement (HMR)? How does it improve the developer experience compared to traditional full-page reloads?
- In your own words, explain what happens when you run
npm run devvs.npm run buildin a Vite project.- What files does Vite generate when you run
npm run build? Where do they go and how would you deploy them to a web server (e.g. in Flask)?
Typescript
- Watch https://www.destroyallsoftware.com/talks/wat, a legendary talk about crazy results from programming languages. The JS examples in particular are a key motivation for using Typescript.
- Read https://www.typescriptlang.org/
- Read https://www.typescriptlang.org/docs/handbook/typescript-from-scratch.html
- Read https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html#types-by-inference
- Read the Typescript handbook:
- https://www.typescriptlang.org/docs/handbook/2/basic-types.html
- https://www.typescriptlang.org/docs/handbook/2/everyday-types.html
- https://www.typescriptlang.org/docs/handbook/2/narrowing.html
- https://www.typescriptlang.org/docs/handbook/2/functions.html
- https://www.typescriptlang.org/docs/handbook/2/objects.html
- (skip type manipulation for now, although the Generics page is useful)
- https://www.typescriptlang.org/docs/handbook/2/classes.html
- https://www.typescriptlang.org/docs/handbook/2/modules.html
Now, check your knowledge:
- What is TypeScript and why might you want to use it over plain JavaScript?
- Why does
[] + []produce the empty string in JS? What does Typescript do instead?- Why does
[] + {}produce"[object Object]"in JS? What does Typescript do instead?- What does it mean when we say that Typescript is “structurally typed”? How is this different from Python, for example?
- What is the difference between optional parameters and parameters with default values in TypeScript functions? How do you define each?
- What is the difference between
interfaceandtypein TypeScript when defining object shapes? When might you prefer one over the other?- What does it mean when a class
implementsan interface? How is this different fromextends?- What is the difference between default exports and named exports in TypeScript modules? Can you mix both in a single file?
- When you import a module, what is the difference between
import * as nameandimport { thing }syntax? When would you use each?
React: Tic-Tac-Toe
- Follow https://react.dev/learn/tutorial-tic-tac-toe to build a tic-tac-toe app.
- To get started, fork https://github.com/dstl-lab/dstl-onboarding-react onto your personal GitHub account,
git cloneyour fork, and runnpm install. - Now, you can start following the tic-tac-toe tutorial starting from the section titled “Setup for the tutorial”.
- To get started, fork https://github.com/dstl-lab/dstl-onboarding-react onto your personal GitHub account,
- One key difference is that we are working in a Typescript file, not a plain JS file. This means that certain steps in the tutorial will produce Typescript errors. The first one you’ll run into is probably in “Passing data through props”.
- You should edit the code so that the errors are fixed. Don’t leave the errors alone, and don’t change the file extension to
.jsx.
- You should edit the code so that the errors are fixed. Don’t leave the errors alone, and don’t change the file extension to
- After you’re done with the main tutorial, complete ALL the suggested improvements (#1-5) for the tic-tac-toe app.
- Make a pull request from your to the original repo (https://github.com/dstl-lab/dstl-onboarding-react). The pull request should include a short screen recording showing how your app works after you implemented improvements #4 and #5 (“highlighting the three squares that caused the win” and “displaying the location for each move in the move history list”).
Now, check your knowledge:
- (Note to Sam: Make sure to give 1 point for each PR)
- (Note to DSTL: Sam will open your PRs and ask you questions about your code. Be ready to justify your code choices.)
- What does it mean to “lift state up” in React? Why did we need to do this in the tic-tac-toe game?
- The tutorial emphasizes immutability when updating the
squaresarray. Explain why we usesquares.slice()instead of modifying the array directly. What benefits does immutability provide?- In the final game, how does the
Gamecomponent calculatexIsNextwithout storing it in state? Why is this approach better than storing it as a separate state variable?- What is the purpose of the
keyprop in the list of moves? What would happen if you used the move description as the key instead of the move index?- When working with TypeScript in React, why do you need to define types for component props? What errors did you encounter and how did you fix them?
- Describe how you implemented improvement #4. What React concepts did you need to use to complete it?