React For Everyone
Click on Project to access the GitHub Project
Abbreviations
*[DOM]: Document Object Model
Understanding Components
starting in React 18, files do not need
import React from 'react';
at the top of the file anymore.there is a transformer inside of babel that removes the requirement to have import React statement
What Are Props
Props are the way that we pass data between parent and children components.
Default Props
Default props are a default value that we can give our props in case a value is not given.
we can also tell out component what type of props to expect
State in Components with useState
In order to use state in a component, you need to import useState
hook then you const [state, setState] = useState(defaultState);
ex.: const [count, setCount] = useState(0);
src/App.js
src/Counter.js
Conditional Rendering
using state
src/Accordion.js
src/App.js
Controlled Inputs
Controlled inputs allow us to take control of our forms and inputs.
strength of controlled inputs is once you get teh value from an input, you can run conditional or any manipulation you really want. See example below.
basically you have control over its value
src/Input.js
src/App.js
Looping in React
React uses the javascript map method to loop through arrays.
src/MoviesList.js
src/App.js
Basic Filtering with State
src/MovieList.js
src/App.js
Component Creation Flow
when two components needs access to the same state, you need to "raise up" state into their parent component and then pass in as props to those child components
when a child component needs to "control" the parent component based on state, you need to "raise up" state to the parent component
reason: everything in React flows down and not up, therefore, state needs to be "raised up" in order to obey that rule of React
src/App.js
src/movies/MovieList.js
src/movies/Movie.js
src/Filter.js
PropTypes
Prop types can help our components know exactly what type of props they should accept and throw errors when the wrong props are sent to them.
make components "smart"
References:
src/App.js
src/movies/MovieList.js
src/movies/Movie.js
src/Filter.js
Refs
refs have changed a lot throughout the lifespan of React but are currently used with the useRef hook.
useRef
returns a mutable ref object whose.current
property is initialized to the passed argument (initialValue
). The returned object will persist for the full lifetime of the component.
Uses:
to get access to the actual DOM and elements on in the DOM
outside the normal React flow
when a ref updates, this does not trigger a rerender of the DOM
can use this to store information and change information without rerendering the component
allows access to actual DOM objects
"dumb" inputs as to the controlled inputs in a previous notes
const varName = useRef(initialValue);
more simple than the filter example above
Disadvantages:
can not control rerenders
no control over the input being used in the text box
src/movies/MovieList.js
useEffect 101
This is a powerful hook and hook helps us to perform actions when a component mount, unmounts, and updates.
basically the ability to trigger side effects
you can tell
useEffect
when to runuseEffect(() => {}, [])
the empty array after the arrow function tells
useEffect
to run only the first time the component renders ( oncomponentDidMount
)if you put some state in the array such as filter, then it would run the
useEffect
when the filter is updated
src/movies/MoviesList.js
React Router v5
React Router reference
Routing is necessary for larger applications with multiple pages
src/App.js
src/movies/Movie.jsx
/src/movies/MovieDetail.jsx