coryhouse / coryhouse/reactjsconsulting
React Fundamentals
- Dominant language
- JavaScript
- Stars
- 374
- Forks
- 33
- PR merge metrics
- No merged PRs in 30d
Description
# React Fundamentals
- [ ] [Y Combinator when React was first introduced](https://news.ycombinator.com/item?id=5789055&ck_subscriber_id=193063445) - Summary, many were skeptical!
- [ ] [React, the Good Parts](https://twitter.com/housecor/status/1256186927025438720)
- [ ] [React = Lego](https://twitter.com/housecor/status/1249291002382364672)
- [ ] [Why JS is eating HTML](https://css-tricks.com/why-javascript-is-eating-html) - Summary: React provides a declarative API that helps assure invalid (out of sync) conditions can't occur. Without React, it's your job to keep the HTML and JS data in sync.
- [ ] Why is React great? Because it changed how we think about applications. We now write apps by defining: “when the data looks like A, the app should look like B”, and “when the user does X, change the data like Y”. We no longer have to write brittle code like: “when the user does X, update these DOM elements to look like B”. In jQuery, we had to manually keep the DOM and state and in sync.
- [ ] [Learn React in 10 tweets](https://mobile.twitter.com/chrisachard/status/1175022111758442497)
- [ ] [A (mostly) Complete Guide to React's Rendering Behavior](https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior)
- [ ] [Why React?](https://app.pluralsight.com/library/courses/react-big-picture/table-of-contents)
- [ ] [History timeline](https://blog.risingstack.com/the-history-of-react-js-on-a-timeline/)
- [ ] [5 Topics to learn before React](https://medium.com/front-end-hacking/5-topics-to-master-before-learning-react-90aa25f5207b)
- [ ] [React developer roadmap](https://github.com/adam-golab/react-developer-roadmap) - Also [see this one](https://roadmap.sh/react)
- [ ] [Components are concerns, not tech](https://speakerdeck.com/didoo/let-there-be-peace-on-css?slide=62). Also [here](https://technology-ebay-de.github.io/breaking-down-your-react-app/#/33).
- [ ] [Think legos](https://technology-ebay-de.github.io/breaking-down-your-react-app/#/11)
- [ ] Library vs framework
## [JSX](https://reactjs.org/docs/jsx-in-depth.html)
- [ ] [JSX - The Other Side of the Coin](https://medium.freecodecamp.org/react-s-jsx-the-other-side-of-the-coin-2ace7ab62b98)
- [ ] [React without JSX](https://reactjs.org/docs/react-without-jsx.html) - call React.createElement.
- [ ] [React components return UI descriptor objects](https://kentcdodds.com/blog/optimize-react-re-renders)
- [ ] [JSX can be assigned to a variable](https://kentcdodds.com/blog/what-is-jsx)
- [ ] [Code in curly braces is left alone](https://kentcdodds.com/blog/what-is-jsx). (interpolation). This allows you to dynamically inject variables into props and children. Interpolation must be JavaScript expressions because they're essentially the right hand of an object assignment or used as an argument to a function call.
- [ ] [String literals](https://reactjs.org/docs/jsx-in-depth.html#string-literals)
- [ ] [Props default true](https://reactjs.org/docs/jsx-in-depth.html#props-default-to-true)
- [ ] [Spread attributes](https://reactjs.org/docs/jsx-in-depth.html#spread-attributes)
- [ ] [Booleans, null, undefined are ignored](https://reactjs.org/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored)
- [ ] [Conditionals](https://gist.github.com/coryhouse/3bb2d1eed7a41f5a87131823b9312085)
- [ ] [Logic-less JSX](https://gist.github.com/coryhouse/b04039708f8dbef6fa233fe9cae9ce37)
- [ ] [Virtual DOM](https://reactjs.org/docs/faq-internals.html#what-is-the-virtual-dom)
- [ ] [Props](https://reactjs.org/docs/components-and-props.html)
- [ ] [Conditionals in render](https://reactjs.org/docs/conditional-rendering.html) - [Many options](https://blog.logrocket.com/conditional-rendering-in-react-c6b0e5af381e)
- [ ] Ternary
- [ ] Logical &&, || in render
- [ ] Multiple return/return early
## [State](https://reactjs.org/docs/react-component.html#setstate)
- [ ] It's async. setState calls are batched. Why? No matter how many setState() calls in how many components you do inside a React event handler, they will produce only a single re-render at the end of the event. This is crucial for good performance in large applications because if Child and Parent each call setState() when handling a click event, you don't want to re-render the Child twice.
- [ ] [An example of state being async](https://codesandbox.io/s/w2wxl3yo0l). Note that alert shows value that was "closed over" when the button was clicked, not the current value in state.
- [ ] To avoid needless re-renders, avoid calling setState when data hasn't changed (or use shouldComponentUpdate)
- [ ] [Approaches for handling state in an immutable manner](https://daveceddia.com/react-redux-immutability-guide/) (or use Immer)
- [ ] Accepts object or function. Functional setState receives prevState and props as params.
- [ ] Accepts callback as 2nd param (but recommended to use componentDidUpdate instead)
- [ ] [Why treat state immutable?](https://reactjs.org/tutorial/tutorial.html#why-immutability-is-important)
- [ ] [Tips for using state correctly](https://reactjs.org/docs/state-and-lifecycle.html#using-state-correctly)
- [ ] [Four immutable approaches](https://medium.freecodecamp.org/handling-state-in-react-four-immutable-approaches-to-consider-d1f5c00249d5)
- [ ] [Where to put React state (flowchart)](https://kentcdodds.com/static/d2b50fdb8371e7ec209faacac5363111/35838/where-to-put-state.png)
## [Forms](https://reactjs.org/docs/forms.html)
- [ ] [Controlled vs Uncontrolled components](https://reactjs.org/docs/uncontrolled-components.html).
- [ ] [Simple uncontrolled form using "the platform"](https://twitter.com/asidorenko_/status/1482679799374098433?s=20) (caveat, no custom client-side valiation)
- [ ] [Example of fast uncontrolled form](https://codesandbox.io/s/form-perf-demo-240ho?file=/src/index.tsx)
- [ ] [Put state inside each form field to improve perf on large forms](https://epicreact.dev/improve-the-performance-of-your-react-forms/)
## [Functional components](https://reactjs.org/docs/components-and-props.html#functional-and-class-components)
- [ ] [The good parts](https://hackernoon.com/react-stateless-functional-components-nine-wins-you-might-have-overlooked-997b0d933dbc)
- [ ] [The bad parts](https://medium.freecodecamp.org/7-reasons-to-outlaw-reacts-functional-components-ff5b5ae09b7c)
- [ ] Each render gets its own copy of nested functions. State values from that render are "closed" over. [Demo of "captured" values](https://codesandbox.io/s/w2wxl3yo0l)
- [ ] Handling events
## Class Components
- [ ] [Lifecycle methods](https://reactjs.org/docs/react-component.html#the-component-lifecycle)
- [ ] [createClass](https://reactjs.org/docs/react-without-es6.html) and [Class components](https://reactjs.org/docs/components-and-props.html#functional-and-class-components)
- [ ] [Lifecycle methods Chart](http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram)
- [ ] Handy concise descriptions in [React Cheatsheet](https://github.com/LeCoupa/awesome-cheatsheets/blob/master/frontend/react.js)
- [ ] [Lifecycle methods visualized](https://stackblitz.com/github/Oblosys/react-lifecycle-visualizer/tree/master/examples/parent-child-demo?file=src/samples/New.js)
- [ ] componentWillMount
- [ ] componentDidMount
- [ ] componentWillReceiveProps -> getDerivedStateFromProps
- [ ] [Binding Patterns - 5 Ways to handle the `this` keyword](https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56)
- [ ] [Avoid arrow functions and bind in render](https://medium.freecodecamp.org/why-arrow-functions-and-bind-in-reacts-render-are-problematic-f1c08b060e36)
## Typechecking
- [ ] Typechecking via [PropTypes](https://reactjs.org/docs/typechecking-with-proptypes.html)
- [ ] [Centralizing declarations](https://medium.freecodecamp.org/react-pattern-centralized-proptypes-f981ff672f3b)
- [ ] Declaring shapes
- [ ] PropTypes vs [TypeScript](https://www.typescriptlang.org/) and [Flow](https://flow.org/). - [Here's a solid comparison of TS and Flow with React](https://levelup.gitconnected.com/flow-vs-typescript-in-react-my-two-cents-d4d0c657d236) And [here's an example of a React component using Flow](https://www.phpied.com/organizing-react-component-h1-2018/)
- [ ] [TypeScript annotations on plain JS](https://medium.com/@martin_hotell/build-100-type-safe-react-apps-in-vanilla-javascript-bd29a8364078)
## Props
- [ ] [Default props](https://reactjs.org/docs/typechecking-with-proptypes.html#default-prop-values)
- [ ] Destructuring props
- [ ] Using Rest with props
- [ ] children prop
- [ ] Setting initial state using a prop
- [ ] Passing components as props
- [ ] Slot pattern
- [ ] [All the props fundamentals](https://www.robinwieruch.de/react-pass-props-to-component/)
## Keys
- [ ] [Keys](https://reactjs.org/docs/lists-and-keys.html)
- [ ] Use when iterating arrays
- [ ] [Use on master/detail](https://twitter.com/sebmarkbage/status/1262937670444974081) - [Here's a good example](https://www.nikgraf.com/blog/using-reacts-key-attribute-to-remount-a-component)
- [ ] [Use to "reset"/create new instance](https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html)
- [ ] [React inline styles and alternatives](https://github.com/coryhouse/rjc/issues/12)
## [Hooks](https://reactjs.org/docs/hooks-intro.html?no-cache=1)
- [ ] [Why Hooks?](https://tylermcginnis.com/why-react-hooks)
- [ ] [Visual comparison of class vs hooks code - Thinking in React Hooks](https://wattenberger.com/blog/react-hooks)
- [ ] [Hooks considered harmful](https://labs.factorialhr.com/posts/hooks-considered-harmful) - Covers how closures give functions state, how react-is determines if the dep array has changed, properly specifying dependency array.
- [ ] [Old componentDidMount/unmount code refactored to hooks](https://sebastiandedeyne.com/forget-about-component-lifecycles-and-start-thinking-in-effects)
- [ ] [Short summary of each Hook's use by Tyler McGinnis](https://twitter.com/tylermcginnis/status/1169667360795459584?s=21)
- [ ] [Summary of each Hook's use on StackOverflow](https://stackoverflow.com/questions/54963248/whats-the-difference-between-usecallback-and-usememo-in-practice/54963730#54963730)
- [ ] useState or useReducer to handle state - [Prefer useReducer to protect state](https://www.builder.io/blog/use-reducer)
- [ ] [Codesandbox with examples of each Hook](https://codesandbox.io/s/z3ow32rk43)
- [ ] Mental model: [Each render has its own everything](https://overreacted.io/a-complete-guide-to-useeffect/#each-render-has-its-own-effects)
- [ ] [Dan's hook intro post](https://medium.com/@dan_abramov/making-sense-of-react-hooks-fdbde8803889)
- [ ] [Simple Hooks example](https://gist.github.com/gaearon/cb5add26336003ed8c0004c4ba820eae)
- [ ] [Sunil's Hooks before and after image](https://twitter.com/threepointone/status/1056594421079261185/photo/1?ref_src=twsrc%5Etfw%7Ctwcamp%5Etweetembed%7Ctwterm%5E1056594421079261185&ref_url=https%3A%2F%2Fmedium.com%2Fmedia%2Fe55e7bcbf2d4912af7e539a2646388e2%3FpostId%3Dfdbde8803889)
- [ ] [Using async/await in useEffect](https://github.com/facebook/react/issues/14326)
- [ ] [useState supports the functional form too](https://javascriptplayground.com/avoiding-recursive-use-effect-hooks-react/) - useful in useEffect to avoid listing state in the dependency array
- [ ] Boilerplate for every data fetch call: 41mins [here](https://www.youtube.com/watch?v=1jWS7cCuUXw) - [Here's a pic of the spot](https://www.dropbox.com/s/yspd7tl617ssmj7/Screenshot%202020-02-04%2007.28.09.png?dl=0) Contrast with UseEffect to see how much simpler it is and how it unifies these concerns.
- [ ] Do I need to specify functions as effect dependencies or not? Options, in order of preference:
- [ ] Hoist functions that don’t need props or state outside of your component
- [ ] [Declare functions that useEffect calls within the effect](https://overreacted.io/a-complete-guide-to-useeffect/#moving-functions-inside-effects). Then it's clear what useEffect depends upon, and you don't have to declare other deps in the dependency array. It helps avoid missing dependencies in the dep array.
- [ ] If after that your effect still ends up using functions in the render scope (including function from props), wrap them into useCallback where they’re defined, and repeat the process. Why does it matter? Functions can “see” values from props and state — so they participate in the data flow.
- [ ] Need to call the func from multiple effects? [Extract it outside the func if possible. If that's not possible, use useCallback](https://overreacted.io/a-complete-guide-to-useeffect/#but-i-cant-put-this-function-inside-an-effect) - that way the function is only recreated when its dependencies change (useCallback accepts a dep array too).
- [ ] [useState pitfalls and how to fix them](https://profy.dev/article/react-usestate-pitfalls)
- [ ] When to prefer useState?
- [ ] Simple state (numbers, ints, strings), and logic with simple state transitions
- [ ] No interest in unit testing state (for instance, if you prefer testing the component via tools like Cypress instead of its internals)
- [ ] No need to pass multiple callbacks deep down the tree
- [ ] Tip: [Group data that changes together in a single useState call](https://reactjs.org/docs/hooks-faq.html#should-i-use-one-or-many-state-variables)
- [ ] If state updates together, use a single useState object. If they update separately, consider separate useState calls.
- [ ] Why prefer useReducer? [blog post on this](https://tkdodo.eu/blog/use-state-vs-use-reducer)
- [ ] It separates reads from writes
- [ ] With useState, you have to remember to use the function form to reliably set state based on current state. With useReducer, you can't mess this up. It's designed for updating values based upon previous state. [Related post](https://adamrackis.dev/state-and-use-reducer/)
- [ ] Easy to test in isolation, since it's a pure function
- [ ] Centralizes state writes, which is often easier to understand
- [ ] Typically simplifies `useEffect` by radically simplifying the dependency array and nested logic as [shown here](https://adamrackis.dev/blog/state-and-use-reducer)
- [ ] Easily handle related state (though admittedly you can also use objects with useState to compose data that changes together)
- [ ] Can easily store entire state tree in localStorage
- [ ] Can pass dispatch down to avoid passing many callback funcs on props
- [ ] Can easily log all actions since they're centralized
- [ ] Makes the component easier to digest by moving state management concerns to a separate file
- [ ] [You can pass an array for the action on useReducer to reduce boilerplate](https://adamrackis.dev/state-and-use-reducer/)
- [ ] Tip: Instead of useState, can do this: `const [state, update] = useReducer((_, next) => next, initial);`. Then can refactor to more robust logic easily. Otherwise, works like useState.
- [ ] Think of useReducer as the “cheat mode” of Hooks. You can decouple the update logic from describing what happened. This helps remove unnecessary dependencies from effects and avoids re-running them more often than necessary.
- [ ] [How hooks correspond to lifecycle methods](https://reactjs.org/docs/hooks-faq.html#how-do-lifecycle-methods-correspond-to-hooks)
- [ ] You can't use async keyword on useEffect. Instead, [extract it to a separate function](https://codesandbox.io/s/jvvkoo8pq3). This isn't a tech limitation. [It's a deliberate design decision by the React team](https://twitter.com/dan_abramov/status/1146879067591008270) so you can cancel handling the async call if the component unmounts before it returns.
- [ ] [Eliminate dependencies by using functional setState in useEffect](https://overreacted.io/a-complete-guide-to-useeffect/#making-effects-self-sufficient), or when setting a state variable depends on the current value of another state variable, you might want to [try replacing them both with useReducer](https://overreacted.io/a-complete-guide-to-useeffect/#decoupling-updates-from-actions). When you find yourself writing setSomething(something => ...), it’s a good time to consider using a reducer instead. A reducer lets you decouple expressing the “actions” that happened in your component from how the state updates in response to them.
- [ ] [Avoid race conditions and memory leaks using AbortController](https://www.wisdomgeek.com/development/web-development/react/avoiding-race-conditions-memory-leaks-react-useeffect/) in useEffect. Another example, linked in React docs: https://maxrozen.com/race-conditions-fetching-data-react-with-useeffect
- [ ] You may omit dispatch, setState, and useRef container values from the deps because React guarantees them to be static. But it also doesn’t hurt to specify them.
- [ ] [Implement lifecycle methods using hooks](https://medium.com/@dispix/from-react-component-to-hooks-b50241334365)
- [ ] [Visual diagrams for how to think about Hooks and why they shouldn't be set in conditionals](https://medium.com/@ryardley/react-hooks-not-magic-just-arrays-cd4f1857236e)
- [ ] useEvent explained in a tweet: https://twitter.com/Huxpro/status/1522273995801776129
- [ ] useTransition is for low priority updates. Returns `startTransition` and `pending`. The returned `startTransition` says "This has a lower priority than other state updates". Typing into an input field should feel instant, so to support that, mark other state updates as low priority (via `startTransition` as needed. [A filtering demo that shows that using useTransition for a filter term means the input stays responsive while the list update lags slightly behind](https://academind.com/tutorials/react-usetransition-vs-usedeferredvalue)
- [ ] useDeferredValue is like useTransition because it's also for low priority updates, but it's useful when the value is coming in "from above"/on props. (so, when your component doesn't have control over the setState call - perhaps a third party lib or package too). Note: You need to use memoization on the component to get any benefit. And the memoization value obviously shouldn't change too often or the benefit disappears.
### useReducer tips: (below are from redux's style guide, but relevant)
- [Do Not Mutate State](https://redux.js.org/style-guide/style-guide#do-not-mutate-state)
- [Reducers Must Not Have Side Effects](https://redux.js.org/style-guide/style-guide#reducers-must-not-have-side-effects)
- [Model Actions as Events, not Setters](https://redux.js.org/style-guide/style-guide#model-actions-as-events-not-setters)
### useEffect Best practices
- [ ] [tkdodo Tips for simplifying useEffect](https://tkdodo.eu/blog/simplifying-use-effect)
- [ ] useEffect is taught too much / too early. It's much more important to teach ways to avoid useEffect: derived state, single source of truth for state, react keys, callback refs, useSyncExternalStore even... know about them and you likely will not need useEffect.
- [ ] WARNING: Almost none of the effects I see in app codebases are syncing with something outside of React. It's always: if prop change, update state, if state changes, dispatch another action, or if state changes, compute something and put in another state. All of these are bad.
- [ ] [useEffect is the `reduce` of hooks](https://twitter.com/TkDodo/status/1558033912886083585). You can do anything with it. You probably shouldn't be using it. There are better abstractions.
- [ ] useEffect runs after render
- [ ] [For useEffect, the question is not "when does this effect run" the question is "with which state does this effect synchronize with"](https://twitter.com/ryanflorence/status/1125041041063665666)
- [ ] Common Pitfalls: Keep dep array as small as possible, use the ESLint hook, profile your app before optimizing
- [ ] [Prefer primitives in dependency arrays](https://twitter.com/housecor/status/1522914348695445505)
- [ ] useEffect is a last resort. It's for synchronizing with external systems.
- [ ] State transitions trigger effects. Effects go in event handlers. Use event handlers to handle actions.
- Prefer primitives in dep array
- Avoid using to sync state that can be derived
- Render as you fetch via suspense.
- Consider modeling effects with state machines
## Component Composition
- [ ] [Component composition](https://reactjs.org/docs/composition-vs-inheritance.html)
- [ ] [props.children](https://reactjs.org/docs/composition-vs-inheritance.html#containment)
- [ ] [Specialization](https://reactjs.org/docs/composition-vs-inheritance.html#specialization)
## Error handling
- [ ] [Catch errors via Error Boundaries](https://reactjs.org/docs/error-boundaries.html) - **Note**: React 16 unmounts the app on error, so it's critical to add at least a top level error boundary to your app.
- [ ] How many? [Identify the feature boundaries in your application and put your error boundaries there](https://aweary.dev/fault-tolerance-react/). Sections that are visually independent are likely independent features which are exactly where you want your error boundaries.
- [ ] Ask: If this component was to crash, should its siblings also crash?
- [ ] Break things on purpose by throwing errors to see what happens.
## Best practices
- [ ] [9 Common beginner mistakes](https://www.joshwcomeau.com/react/common-beginner-mistakes)
- [ ] [The 8 Key React Component Decisions](https://medium.freecodecamp.org/8-key-react-component-decisions-cc965db11594)
- [ ] [React component code smells](https://antongunnarsson.com/react-component-code-smells/)
- [ ] [Common mistakes](https://dev.to/samerbuna/reactjs-frequently-facedproblems--l5g)
- [ ] [More from Elijah Manor](https://twitter.com/elijahmanor/status/994681956444000256) and [this gist](https://gist.github.com/elijahmanor/778c8effcd62db9a1a5a78899fdd68de)
- [ ] Nice summary of fundamentals: [react-from-zero](https://github.com/kay-is/react-from-zero)
## Quiz
1. How does JSX differ from HTML?
2. Is JSX required?
3. What tool compiles JSX?
4. What method is required on all class components?
5. What is a lifecycle method? Can you name one?
6. What is destructuring? Why do we do it? Contrast object destructuring with array destructuring.
7. Why prefer const/let over var?
8. What tool did we use to create our React app?
9. What Hooks did we use? What were they for?
10. Do lifecycle methods exist in function components?
11. What tool is checking our code quality/standards and checking our code for common errors and issues?
12. How do we declare data that changes over time?
13. What’s the term for passing config settings into a component?
14. How do we declare what values our component accepts?
15. When do you declare a key? Why declare a key? What’s a good key? What’s a bad key?
16. How do you run an npm script?
17. Why is client-side routing preferable to server-side?
18. How do you make a function public?
19. How do arrow functions differ from regular functions?
20. How do you copy an object in JS? Is it deep or shallow?
21. When does React re-render?
22. How do you declare state in a class? A function?
23. What tool are we using to automatically format our code?
24. What does JSX compile down to?
25. What tool are we using for our mock API? How did we configure it?
26. How did we automate starting our app and API at the same time?
27. What array method did we use to iterate over the list of users? What does it return?
28. What is a predicate?
29. What is a higher order function? List some higher order functions on the array prototype.
30. How do you copy an object in JS? Is it deep, or shallow?
32. What is a wildcard import? When is it useful?
33. What are the benefits and downsides of using a default export?
35. Does const make an object immutable?
36. Name 2 ways to declare a React component. Which should you prefer?
37. What is the current version of JS? How often are new versions released?
40. When is the rest operator useful?
42. What’s the name for arguments passed to React components?
43. How do we specify the types for each argument?
44. How did we debug?
45. What hook did we use to run code after render?
48. What’s a promise? How do we handle a successful promise?
49. How do we handle an error from a promise?
50. When you copy an object in JS, is it deep or shallow? What’s the difference?
51. Why is client-side routing preferable to server-side?
52. Is the code we see in the browser what’s actually running?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.