IMY220_L5_NodeJsReactIntermediate
IMY220_L5_NodeJsReactIntermediate
IMY220_L5_NodeJsReactIntermediate
Intermediate
IMY 220 ● Class Discussion
Quiz!
https://quiz.com/77046fbc-0b36-4882-84e1-48326ed70858
⭐ Component State - Exercise
Note: Using babel CDN (do NOT do this in production - very slow!)
Use the same files off of ClickUP. In the file called App.js implement (10-15mins):
- A component called AddContact that contains a form to add contacts to the list. It should
define a function called addContact that it uses to add contacts to the list by getting the
form values (hint: ref), and then calling onContactAdded that is passed in as a prop.
- A component called App that acts as the parent component.
- It should have two children: AddContact and ContactList.
- It should take in a list of contacts as its prop and use this to initialize its state. It should pass this state to its
child ContactList.
- It should also define a onContactAdded function which updates its state (adds a contact to the array). It should
pass this function to its other child AddContact.
Render the App component to index.html, passing it the provided list of contacts.
⭐ Components - Exercise
Final structure should look like this:
Babel
“Babel is a tool that helps you write code in the latest version of JavaScript.
When your supported environments don't support certain features natively,
Babel will help you compile those features down to a supported version.”
https://github.com/babel/babel
- Babel also contains presets that allow you to use other syntaxes as
well, such as JSX
- Babel can thus be used to compile JSX code into valid JS code.
- Babel also requires a configuration file called .babelrc to work. This
tells Babel to transpile all ES6 code by default using its environment
preset (Babel has many other presets, including the react-preset)
Webpack
Webpack takes one/more files to be bundled and optionally uses some
plugins, loaders, transpilers, etc. to deliver an output file that is usable by
our main webpage files.
Similar to Babel’s .babelrc file, Webpack requires a configuration file to
work properly, called webpack.config.js
Node + React
A better way to do this would be to create a separate file for each
component and to import the different components as each file needs it.
This way, each component is logically separated into its own file and
index.js simply renders the main component.
Node + React
1. Move each class declaration into its new file. (e.g. Person.js)
2. Also export the class declaration for the component. We could do
this using default exports, but since this is somewhat of a bad
practice, we’ll export the class definition instead.
Node + React
// Contents of AddPersonForm.js inside the new "components" directory
Person.propTypes = {
person: PropTypes.object
}
Prop Types
If we now try to create a Person component with a person prop that is
anything except a JS object, we’ll get a descriptive error
The Prop Types library has many built-in type checks, such as:
• PropTypes.array
• PropTypes.bool
• PropTypes.func
• PropTypes.number
• PropTypes.object
• PropTypes.string
• PropTypes.symbol
Prop Types
It is also a good idea to validate the existence of a required prop using
PropTypes.isRequired
You can also chain Prop Type validation and add validation for multiple props
simultaneously, for example:
Person.propTypes = {
person: PropTypes.object.isRequired,
example2: PropTypes.func.isRequired
}
You can find many more examples of Prop Type validation here:
https://react.dev/reference/react/Component#static-proptypes
⭐ PropTypes - Exercise
Note: Using babel CDN (do NOT do this in production - very slow!)
Use the same files off of ClickUP. In the index.html file, implement (5 mins):
- Add Proptypes to the Pet component so that the props that are passed in can
be checked.
- You can assume the given props are correct.
- Hint: Find out how to validate objects with fields here:
https://www.npmjs.com/package/prop-types
- All of these props, besides the adoption price, are required.
After you have written this, mess with the props passed in to see the results of
passing in the wrong props.
How Routes and Requests Work
Recap from COS216
[...]
https://reactrouter.com/en/main/start/overview
How React Router Works
1. Intercepts server requests for ‘pages’ e.g.,
“../home”, “../about”, “../contact”, etc.
2. Renders the corresponding component
(tree) for each route that’s needed e.g.,
<Home/>, <About/>, <Contact/>, etc.
a. I say ‘tree’ because these components can have
child components.
3. Updates the DOM with the relevant
components, without needing to re-render all
of them (i.e., reload the page)
- Demo
- Setting up the ‘pages’ (components)
- Installing React Router
- Setting up the BrowserRouter component
- Setting up the routes (Routes and Route components)
- Setting up links (Link component)
- Routing in action
Alternate React Router Syntax (New v6+ Method)
const router = createBrowserRouter([...]);
- You may see this syntax being used along with a RouterProvider
component
- It functions practically the same as the method we just used, but is the newer
way of doing things (v6.4+) and is recommended by React Router in their
documentation because more properties / APIs you can use with it besides
path and element
- Instead of <Route path=”” element=””/>
- You’ll have { path: “” , element: “”, …etc. }
Dynamic Routes in React Router
Called ‘Dynamic Segments’
- e.g., ‘/products/:id’
- Everything else is the same as we have
done thus far
https://stackoverflow.com/questions/585
48767/react-router-dom-useparams-insi
de-class-component
Cookies in React
IMY 220 ● Lecture 5
Cookies, Session & Local Storage Basics
COS 216 Recap:
1. Cookies
2. Session Storage
3. Local Storage
Differences?
https://medium.com/@dimplekumari0228/describe-the-difference-between-a-cookie-ses
sionstorage-and-localstorage-e731a627acb1
Cookie/Session/Local Storage Setup in React
Same as in a regular HTML & JS project.