IMY220_L4_ReactBasics
IMY220_L4_ReactBasics
- React Basics
- Creating components
- Using components inside other
components
- Props
- Types of components
- Basic component state
“JavaScript frameworks are an essential part of modern front-end web development, providing developers
with tried and tested tools for building scalable, interactive web applications.
Many modern companies use frameworks as a standard part of their tooling, so many front-end
development jobs now require framework experience.”
https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Client-side_JavaScript_frameworks
https://react.dev/learn/thinking-in-react
Components
Components
Components
Components
Creating Components
class ComponentName extends React.Component {...}
1. Capital letter (very important, React will sometimes try render an HTML
element otherwise - also convention)
2. extends React.Component (using some of the basic React component
library functionality - same thing as inheriting from a C++, Java or JS class)
3. {...} (JS class functionality goes here - with some added functions React
provides in the base class (we’ll get to this in later lectures))
Creating Components
Two must-haves:
constructor()
render()
Use the files off of ClickUP. In the file called App.js, implement (5mins):
Important:
- Only one root element (React.createElement function under the hood only takes one
root parameter)
- To not bloat the HTML with <div>s, use React fragments
- OLD method in videos: <React.fragment>
- ⭐⭐⭐ NEW method: <Fragment></Fragment> or <></>
Using Components
Modify state:
- Must call setState() NOT modify state variables directly (i.e., like vars)
- React handles the state for you - automatically updates the elements for you
whenever you call setState() - will NOT work if you try modify directly
- Complexities: Batch updates, value comparison, etc. (Future Lecture)
Component State - Events
JS event handlers to handle user input
- onclick => onClick (React version of the event handler)
- Define the function used onClick in the class (self-contained functionality)
JS Class functions not automatically bound to class (does not have access to
component’s this)
- Have to manually bind them
this.refName = React.createRef()
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:
Types of Components (in the Wild)
Functional components
- Newer
- Generally preferred (even by React
themselves)
- Two forms: function()= and
const = () =>
Class components
- Simpler to understand (OOP)
- Still run into some of the same errors if
you use functional over class
- Recommended for you
Types of Components (in the Wild) - Differences
Class Functional
Types of Components (in the Wild) - Differences
Class Functional
Troubleshooting React 🔥🔥🔥
You are likely to run into issues when
dealing with React State and
Asynchronous behaviour
- COS 226 is your friend when
understanding WHY these issues
arise.
- I will try my best to guide you through
some of these issues when we get
there.
- Lots of frameworks exist for
managing React’s state for more
complex applications.
- For our simple application (semester
project) you will be fine without them.
Extension Exercises We Don’t have Time for: