These are my reading notes for Code Fellows
Note: As with day 2, I accidentally read all of the Intro to React page for Read 01. My notes for that particular reading can be found in the day 1 reading here.
Calculator
contains the ‘shared state’, so it can influence both celsius
and fahrenheit
. It becomes the “source of truth” (quote taken from reading)
Calculator
can make both celsius
and fahrenheit
consistent with each otherRemember that props
are Read-Only
onChange
, onTemperatureChange
, onNameChange
, onTargetChange
- new syntax
A couple examples of these changes, as discussed in the reading:
this.state.value
becomes this.props.value
this.setState()
becomes this.props.onValueChange()
The inputs stay in sync because their values are computed from the same state (source)
Every piece of data that is able to change should have one source of truth
If more than one component of your code needs to access this state, you lift it up to the parent they have in common (keep hierarchical top-down flow in mind)
Using this method greatly reduces the possibilities of bugs!
As we learned in today’s code challenge, the map()
function can loop through an array
The code to display any array of numbers to a ul
/li
element is insanely dry:
const exampleNumbers = [2, 4, 6, 8, 10];
const showItems = exampleNumbers.map((exampleNumbers) =>
<li>{exampleNumbers}</li>
);
ReactDOM.render(
<ul>{showItems}</ul>,
document.getElementById('root')
);
The above code displays the following (although it omits the highly-suggested key. Key’s give elements a “stable identity”):
- 2
- 4
- 6
- 8
- 10
<li key={}>
as an example). The Article also strongly recommends against using indexes as keysA[nother] good rule of thumb is that elements inside the map() call need keys.
(source)
The spread operator is a useful and quick syntax for adding items to arrays, combining arrays or objects, and spreading an array out into a function’s arguments.
(source)
...
Trying to find the largest number in an array like this:
Math.max([2,8,55])
Would result in “NaN”. Using a spread operator like this:
Math.max(...[2,8,55])
Would correctly produce “55”!
const fruits = ['🍏','🍊','🍌','🍉','🍍']
const moreFruits = [...fruits];
console.log(moreFruits) // Array(5) [ "🍏", "🍊", "🍌", "🍉", "🍍" ]
fruits[0] = '🍑'
console.log(...[...fruits,'...',...moreFruits]) // 🍑 🍊 🍌 🍉 🍍 ... 🍏 🍊 🍌 🍉 🍍
The spread operator is super helpful with concatenating separate arrays
I am absolutely blown away by how cool this is!