Dont Forget React
Dont Forget React
The “Virtual DOM” checks the old “Virtual DOM” – Changing the virtual
DOM is much faster, because it’s just a JavaScript object.
WHAT
OLD VIRTUAL DOM CHANGED? NEW VIRTUAL DOM
<body>
<div> <div>
Update this:
DOM HTML
[ ]
We no longer use the
document. methods
REACT directly
React does that for us.
All tags must now have a (self) closing tag (for example, input and img)
Some properties change, for example class becomes className=, and style= now
takes a javaScript object instead of a string.
function MyComponent() {
return <p>This is JSX!</ p>
}
Using JSX, we can create “custom” elements, with their own structure, properties,
logic, and styling. In React these are known as components. Components must
return JSX.
function MyComponent() {
return (
<form onSubmit={...} >
<input type=”text” onChange={...} />
<button onClick={...} >Click me! </button>
<input type=”submit” />
</form>
)
}
They take a function as a value, Don’t Call this Function! Use an anonymous /
wrapper function if necessary.
<input
type="text"
onChange={e => console.log
(e.target.value)} /> logs everytime we type
The “event object” (e) will contain the data you need for “onChange” in a text input, ex:
<button
onClick={ ( ) => setClicked(true) }
>
Click me sets state variable on click
</button>
Components
Think of Components as “Custom JSX Elements” that we can define, and then use one
or more times. Ultimately, the components are just like variables which will be
converted to HTML.
JS MyComponent.js
Defining a component
JS App.js
<> Index.html
As above, our components will go inside one another, and we can use
them multiple times.
<All MyComponents/>
CREATE
APP
You can install additional libraries and dependencies, like React Router or
Material UI, with “npm install”
{ } package.json
"dependencies": {
"react": "18.0.0",
"react-dom": "18.0.0",
"react-scripts": "4.0.0"
},
Then, these will be added node modules folder, which contains all your library code.
(You don’t commit this to GitHub)
These will be added to package.json, which is the “assembly manual” for your app...
Transpiling
Browsers can’t run React code, so it must be TRANS-piled to vanilla JavaScript,
HTML and CSS. There are libraries that do this for us. Babel and Webpack.
Transpiling takes all our .JSX or .JS files (React can be written in both) and, by
default, puts them into a single large bundle.js file
App.js
Component1.js Bundle.js
Component2.js
Loading these big files over a network can really slow things down
Having one massive bundle.js will slow down our initial page load, however
bundlers have improved on this over time with things like:
Props
The “arguments” of a component, they’re how we pass data from one component
to another.
Think of them as “custom properties” – we send them with the “key=value” syntax
And receive them with “props” object – so they can be accessed accordingly.
function AllMyComponents ( ) {
return (
<>
<MyComponent name="Aaron" />
<MyComponent name="Jack" />
<MyComponent name="Jan" />
</>
)
}
In the tree model, passing props might look like this ...
<All MyComponents/>
We create them with useState hook, that takes an “initial value” returns an array of 2
myName will become the initial value “Aaron”
setMyName("Jack");
But cannot assign anything directly to state (always use the setter function!)
Changing state will “update” the component it lives in, and all components
it’s passed to.
State is IMMUTABLE. If we want to update an array, we need to create a new copy
We often useEffect to fetch some data once and only once, so we aren’t
unnecessarily bombarding our server.
It takes a function as the first argument, (this cannot be an async function)...
We often useEffect to fetch some data once and only once, so we aren’t
unnecessarily bombarding our server.
It takes a function as the first argument, (this cannot be an async function)...
React.useEffect( ( ) => {
fetch("https://myserverurl.com/items/" When itemID changes,
+ itemID ) we run fetch again.
.then(...)
}, [ itemID ])
React.useEffect( ( ) => {
return ( ) => alert(" goodbye ! ") Only runs when
}, [ ]) component gets
removed from DOM
<h1>
</h1>
Component Libraries
There are tons of component libraries, but the 2 most common are React Router,
and some form of UI Library
React Router
React Router enables us to do “Client Side Routing”, so we can have some
navigation in our React apps with multiple pages and paths that can be
observed in the URL bar.
index.html Faq.html
<h1>Hello there!</h1> <h1>FAQ</h1>
<a href=”faq.html”>Go to <a href=”index.html”>Go to
faq</a> home</a>
https://mywebsite.com/index https://mywebsite.com/faq
While in HTML, we’d need a different file and server request for each path...
App.js
import {
BrowserRouter,
Routes,
Route,
} from "react-router-dom ";
import Home from "./Home"
import Faq from "./Faq"
function App() {
return(
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="expenses" element={<Faq />} />
</Routes>
</BrowserRouter>
);
}
Home.js Faq.js
import { import {
Link Link
} from "react-router-dom "; } from "react-router-dom ";
export default function Home() export default function Faq()
{ {
return( return(
<> <>
<h1>Hello there!</ h1> <h1>FAQ</h1>
<Link to="/faq"> <Link to="/">
Go to FAQ Go to Home
</Link> </Link>
</> </>
); );
} }
UI Libraries
Let you use pre-built and styled Components in your apps,
that you can customize. This saves a massive amount of
[ ]
API is just shorthand
time. for “interface” or how
you interact with
You will have to have the docs for your library of choice something from the
outside, whether it’s
open to learn what components are available As well as a back end server,
their “APIs” – but in this context, the API is a component’s function, or React
props. component, you can
use this term.
Visit
MUI.COM