A Visual Guide to State in React
With Kent C. Dodds –
- Intro
- A UI with Vanilla JS
- A UI with React
- Create a UI with React + JSX
- Use JSX effectively with React
- Render two elements side-by-side with a fragment
- Create simple reusable React Compoment
- Validate Custom React Component Props with proptypes
- Understand and Use interpolation in JSX
- Render a React Application
- Style React Components with ClassName and inline Styles
NOW, with 30 SECOND SUMMARIES
- Intro
- Best to watch and take notes first
- A UI with Vanilla JS
- Just like we’ve always done. Create an element in the DOM. use document.CreateElement() to attach that element to the dom.
- A UI with React
- Instead of
document.CreateElement()
we useReact.createElement()
- When we use React.createElement() we can pass props! so `React.createElement(‘div’, {children:[‘child1’,’child2’], className:[‘container]})
- Instead of
- A UI with React + JSX
- We need babble to compile our JSX into javascript
- Effectively use JSX with React (REWATCH THIS ONE OMG)
- you can pass props around with the spread operator
{ ...props}
- you can pass props around with the spread operator
- React Fragments
- Lets you put two html elements next to one another
- Simple Reusable React Component
- I can pass a function to React.createElement() simply by making it uppercase
- Validate Custom React Component Props with PropTypes
- import this npm library
- It is not included in product
- .isRequired() makes the proptype required
- Definitely use this to make sure people are using your components correctly
- Understand and use INTERPOLATION in JSX (ALSO REWATCH OMG)
- Everything in {} is going to be EVALUATED. Meaning it can only be an expression – NOT a function (no for loops etc)
- We can remember this by actually envisioning the
React.createElement()
– we would never pass a for-loop to this, would we?!
- Rerender a React Application
- This was our timer application
- Style React Components with className and inlineStyles
- This was our three differently sized boxes
- Use Event Handlers with React
- We can use
onClick()
andonFocus()
andonMouseOver()
andonBlur()
andonChange()
- We can use
- Manage State in a React Component with the useState hook
- State stores everything in an ARRAY!!! A state array!! So it is essentially
stateArray = React.useState()
andname = stateArray[0]
andsetName = stateArray[1]
- State stores everything in an ARRAY!!! A state array!! So it is essentially
- Manage side-effects in a React Component with useEffect hook
- This was adding local storage to our form
- use a lazy Initializer with useState
- To avoid unnecessary re-renders with useState, add an arrow function so it ONLY renders when absolutely necessary (the first time, instead of every time state changes)
- Manage the useEffect dependency Array
- Dependency Array! The second parameter we can pass to useEffect to make sure it only runs when that piece of state updates
- Create reusable custom hooks
- useLocalStorageHook()
- Manipulate the DOM with React refs
- VanillaTilt library (mounting and unmounting)
- use useEffect() (with an empty dependency array!)
- Add a function after to remove it from the dom so we can appropriately garbage collect
- Understand the React Hook Flow