React Notes
React notes
Uses .jsx, a version of javascript lets you write html within js and javascript, within { }, e.g. “{‘this is treated as JavaScript code’ }
Must be compiled into javascript with a transpiler like Babel.
React is called Electron on desktop and React native on mobile. For web - react.js or just react.
## JSX
NB can’t write class in jsx - have to write className because class is a reserved word in js.
Often use variables to set attributes, e.g.:
this can be useful when setting images, or referencing an object, like so:
Building a React App
Common Build tools
-
Create React App
npx create-react-app
creates react app and bundles it with webpack automatically.- html element will just have a root div, linked to index.js which tells what to feed in and app.js which has all the html in.
npm start
will run live server with React app.npm run build
will crerate a build folder which can be uploaded to server.
-
Vite
npm init vite my-app
- Simplifies and speeds up build process.
- works for most developers and with multiple frameworks.
- can add plugins
npm run dev
to run locally.- Makes typescript faster.
- modifying source code will make changes instantly, unlike create react app.
npm run build
to generate build version
-
Next.js
-
Gatsby
Files in a typical React app
package.json
- dependencies and settings.node_modules
- source code for node dependencies. git ignore and don’t touch.public
- where static files are storedsrc/index.js
-main entrypoint to start appsrc/App.js
- root component of app.src/App.spec.js
- unit tests for app, to show if app working correctly.src/*.css
- css styling for app.
Components
-
Use React Developer tools extension to see React components in dev tools
-
each component has to be children nested in parent element, not just children. e.g. put paragraphs in a div.
N.b need parentheses around multi line jsx: e.g.
Click me!
```Two ways to create component
1. Functional components
n.b. functions have to start with capital letter - i.e. DemoComponent here
- This lets us make complicated layouts with lots of different components.
- This is most popular method of making components, more popular than below method.
If you want a React component within another function componentn, you define define component and render it as html element within other component’s body:
e.g.
2. Class components
- This isn’t really used anymore - much easier to define with a function.
-
It uses
super()
to call the constructor of the parent class, in this caseReact.Component
. -
can compose components together by giving custom named parent HTML tag, e.g. in render you can write
- You can render class components within other elements in the same way. E.g. just nest
<className></className>
Props
props - short for properties
Use props to pass data into components
can be string, number, object, or another react component.
Similar to using a global variable in javascript.
put in curly brackets so don’t need . notation.
props go from parent to child.
{props.children} gives inner html of parent component
props passed to class component are available as: this.props
State
- State is any data that changes over time within a given component. Used to update UI when data changes.
e.g.
state is updated when setCount
is called. setCount
takes a function as an argument.
- Can pass state from parent to child with props but cannot pass state from child to parent. Must edit state in parent.
Lifecycle and events
mount, update, unmount from component tree
e.g. in class component:
You can’t do this in functional components, instead you can use useEffect
:
takes function as an argument, then array of dependencies then another function when destroyed, e.g.
Context
- Context is a way to pass data through the component tree without having to pass props down manually at every level.
- Happens on API level with a function called
createContext
. - e.g. :
- use with
useContext
## Error Boundaries
Not often required but can be useful for debugging and making sure everything is working correctly.
React Query
- used to fetch data
- e.g. :
Hooks
React Hooks mean we can have state in Function component.
Watch this: https://www.youtube.com/watch?v=f687hBjwFcM
Comments in jsx
adding to DOM
to add to DOM, use React’s rendering API:
ReactDOM.render(componentToRender, targetNode)
where variables are componentToRender - ie. component from .jsx and targetNode, the element we want to send the component to. Can use document.getElementByID to select this target element.
for React components (as opposed to JSX) - you have to use the <component></component>
or <component/>
syntax - for both class components and functional components
Tags
all tags have to be closed . all elements can be self closing too, such as <div>
, can be written as <div />
or <div></div>
.
Conditional rendering
Very common, based on boolean.
You can’t write if else statements in JSX. Instead, use:
- Outside JSX, in javascript curly brackets e.g.
Here the javascript runs before the JSX code.
- Using Ternary expressions:
This is great when you have two conditions to choose between.
- Logical And
This is great when you want to either show something, or nothing at all. It can also be used for two conditions though a bit more complicated than ternary operator.
Loops
Most common way to loop in React is with array map:
Here the key attribute gives each element a unique identifier so React can keep track of it in case the name value changes.
Events
Usually use getElementById and event listener in vanilla js, but in React we use event handlers.
We can then define the handleClick function and pass it to the onClick attribute.
We could also do this all in the onClick attribute:
can pass a function as a prop from parent to child too.
React Bootstrap
Predesigned components in HTML, CSS and Javascript
Resources
-
Best place for reading about React: https://react.dev/learn
To read more: