React Fundamentals Welcome
- Change the
element
fromdiv#root
top#greeting
- To create a div DOM node with the text “Hello World” and insert that DOM node into the document.
- (In our HTML document,) add a
<body>
tag with a<div>
inside of it. - Add the
id
"root"
. - We can test at this stage by adding some text.
- (Below this
<div>
,) add a<script>
tag that istype="module"
.- (Note that everything in this tag is JavaScript)
- (In our
<script>
,)- Create a div element.
- Assign it’s reference to the variable
element
- (Use that reference to) set
textContent
. - (To make sure we got this far, we can
alert(element.textContent)
) - Perfect.
- Now set the
element
’sclassName
tocontainer
- (With our new
"Hellow World"
Let’s append our new"Hello World"
element to our"root"
div and append ourHello World
element to it.- Query the dom for the
div#root
- Store a reference to that element in the varible
element
. - Use that reference to append the “Hello World” div
element
- Query the dom for the
- Let’s step back and look at what we’ve done.
- We have an HTML Document which we added 3 tags to:
body
,div
, andscript
. - We have our
div
an idroot
so we could easily access it via JavaScript — later. - In our script we used DOM APIs to:
- Create an element
- We assigned text and a className to it
- And append it to our
div#root
element
- You can learn a lot more about the browser’s DOM APIs at MDN, a great resource for all web and JavaScript knowledge
Extra Credit
- To create the root element in the script section,
- Delete the
<div id="root">
HTML tag - (Because
div#root
no longer exists,) Change therootElement
fromgetElementById("root")
todocument.createElement("div")
- Use the
setAttribute
method to setid
toroot
. - The append stays the same.
- Be sure to remember that
createElement
,getElementById
,setAttribute
, andappend
are all part of the browser’s DOM API. The best place to learn more about browser DOM APIs on MDN.
Ideas from
-
- Raw React & JSX
- Recommended VS Code / DevTools Extensions
- Tools and how to use them
- Keyboard shortcuts & scripts
- How you organize your screens when developing
- Editor, browser preview, terminal, etc.
- Compare raw DOM manipulation vs. React
- Comparing createRoot() and render()
- JSX children strings vs. elements
- Interpolation tricks
- When to spread props vs. pass individually
- Naming conventions and when to use them
- Philosophy of using props vs. composing children
-
- Styling & Forms
- Common gotchas that affect performance
- Assigning types to props with TypeScript
- When to use fragments or not
-
- Rendering Arrays
- Importance of the key prop when rendering lists
- Resetting component stat by changing keys
- Fixing “Uncontrolled input to controlled” error
- Controlled input values managed by React state, Uncontrolled by DOM state
- Show how error occurs when value changes from undefined
- Explain how to decide when to use which
-
- useState and useEffect Hooks
- Watch Local Storage for changes
- To useMemo or not to useMemo?
- Examples of when to use a different hook instead
-
- Tic-Tac-Toe part 1
- Quick refactor a class component to hooks
-
- Tic-Tac-Toe part 2
-
- useRef and useEffect
- Example of useRef
- Tracking mount state or storing references to DOM elements
- (Different example than vanilla-tilt)
- Using useEffect for an HTTP Request
- Handling an async callback
-
- Error Boundaries
- What is an Error Boundary?
- Include error reseting
-
- useReducer & useCallback
- Comparing useLayoutEffect to useEffect / other hooks
-
- useReducer
- Redux-style useReducer
- Other uses for useReducer
-
- Context
- Context & useContext vs. Redux style useReducer
-
- Advanced Context
- What makes a component future proof?
-
- Flexible Compound Components
- Handling error bubbling effectively