React is a fantastic tool for breaking a large project into small atomic re-usable pieces known as "components"
React allows you to write HTML inside Javascript using a special syntax called JSX
JSX is not native to browsers, it must be compiled into HTML and JS, this is handled by libraries that we will be bringing in.
In order to render react code, you will need both the react and reactDOM package.
ReactDOM.render(what_to_render, where_to_render) is how react "mounts" the DOM. It takes two arguments, what to render, and where to render
what to render- This is always a jsx element/component
where to render - An html element, typically a div with the id root
event listeners in react are added onto JSX elements the same way you would assign an attribute in html. The only difference is the casing should be camelCased -> <div onClick={handleClick}>
React components are javascript functions that return JSX (There are other types of components but we won't be covering them as they have fallen out of favor over the years)
To render a component, you would use it similar to a self-closing html tag -> <MyCounter />
In order to use javascript in the return of a react component, you must use the special jsx expression syntax denoted by a set of {}
the text content inside of the h1 will be treated as plain text
the text content inside of the h2 will be treated as javascript i.e the + operation will be performed
The only valid data types in jsx are string | array | jsx. Anything else will need to be wrapped inside a jsx expression otherwise it will be either ignored or coerced to a string
React State
const [reader, writer] = useState(defaultState)
state is a way to update react components by causing a re-render
useState returns an array of two elements, the reader and writer
reader is the current value of state
writer this is a function that updates state. -> writer(newState)
The argument passed into useState is the default value for state.
const Counter = () => {
const [currentNumber, setCurrentNumber] = React.useState(0);
return <h1>I am a counter. My current number is {currentNumber}</h1>
}
currentNumber is our reader
setCurrentNumber is our writer
0 is the default value i.e the value that currentNumber will startoff as
Everytime setCurrentNumber is called, the currentNumber will change accordingly
const Counter = () => {
const [currentNumber, setCurrentNumber] = React.useState(0);
const updateCurrentNumber = () => setCurrentNumber(100);
return (
<div>
<h1>The current number is {currentNumber}</h1>
<button onClick={updateCurrentNumber}>Change number to 100</button>
</div>
)
}
Clicking on the button will call setCurrentNumber passing in 100. When this happens the component will re-render and after the re-render finishes, currentNumber will be updated to 100
Any properties that were previously kebab-cased like column-gap should be camelCased for JSS i.e columnGap
All values should be stringified. The only exceptions are numbers, but these will always default to pixel.
Parcel
Parcel is a bundler that allows you to combine your code and all its dependencies into one file.
Parcel is usually added a dev dependency because it is not actually required in your code, but it just allows you to run the app locally -> yarn add parcel -D
The command to run a file using parcel is parcel file_name
import React from 'react'
Using parcel we have access to the latest javascript syntax, one of which is "module exports". This allows you to use the special import and export syntax that is not available in node. This is functionality the same as require
Webpack and Parcel are tools that we will be using to pre-compile our React code before runtime, to improve performance.
Just like vanilla javascript event listeners, you must pass a function as the value. This can be a named function or an anonymous function -> <div onClick={() => console.log('anonymous func')}>
The argument that will always be passed in when the listener is triggered/handled is an event object.
Typical use cases for the event object may be to derive the value of an input's change evnet -> event.target.value or to prevent the default behavior of a form submit event.preventDefault()
Its important to note that the onSubmit event goes on the form tag and NOT the submit button
The only real difference between jsx and vanilla javascript is the casing for the listeners and the {} jsx expression syntax.
Something you can leverage in react is the value attribute on certain input types. You can passed a controlled value to an input element. A controlled value is a state value i.e a value that you control via state.
08/24 Day 20 Lecture Notes
React Intro
JSX
render
react code, you will need both thereact
andreactDOM
package.ReactDOM.render(what_to_render, where_to_render)
is how react "mounts" the DOM. It takes two arguments, what to render, and where to renderroot
JSX
elements the same way you would assign an attribute in html. The only difference is the casing should be camelCased -><div onClick={handleClick}>
className
React Components
JSX
(There are other types of components but we won't be covering them as they have fallen out of favor over the years)<MyCounter />
App
rendersHeader
,Content
, andFooter
. ThenReactDOM.render
only rendersApp
, which will subsequently render all the childrenJSX Expressions
{}
h1
will be treated as plain texth2
will be treated as javascript i.e the+
operation will be performedstring
|array
|jsx
. Anything else will need to be wrapped inside a jsx expression otherwise it will be either ignored or coerced to a stringReact State
state
is a way to update react components by causing a re-renderuseState
returns an array of two elements, thereader
andwriter
reader
is the current value of statewriter
this is a function that updates state. ->writer(newState)
useState
is the default value for state.currentNumber
is our readersetCurrentNumber
is our writer0
is the default value i.e the value thatcurrentNumber
will startoff assetCurrentNumber
is called, thecurrentNumber
will change accordinglybutton
will callsetCurrentNumber
passing in100
. When this happens the component will re-render and after the re-render finishes,currentNumber
will be updated to100
Javascript Stylesheet (JSS)
column-gap
should be camelCased for JSS i.ecolumnGap
Parcel
yarn add parcel -D
parcel file_name
import
andexport
syntax that is not available in node. This is functionality the same asrequire
Event Listeners / Event Handlers / Forms
<div onClick={() => console.log('anonymous func')}>
event
object.event
object may be to derive the value of an input's change evnet ->event.target.value
or to prevent the default behavior of a form submitevent.preventDefault()
onSubmit
event goes on theform
tag and NOT the submit button{}
jsx expression syntax.value
attribute on certain input types. You can passed a controlled value to an input element. A controlled value is astate
value i.e a value that you control via state.Props
props
short for "properties" is how data is passed from parent -> child in react.props
are very similar to html attribute. They are even assigned the same way, on the component'stag
props
follow a key value pair -><SomeComponent key="some value">
key
and the value is the stringsome value
props
, it is accessible in the function's parameterprops
props
parameter will always be supplied. But it will be an empty object untilprops
are passed from the parent.App
(parent) is passing uniquetitle
prop to each of theCounter
s, i.eburger
,coffee
, andchicken sandwich
Counter
component to access thetitle
prop, it simply needs to reference it by callingprops.title
props
are a great way to make the same component unique and reusable!