Above is another example of passing / recieving props.
Passing props is done the same way you'd assign attributes to an html tag -> <Turtle someProp="some value">
Accessing props is when you reference the function parameter and access desired property -> props.someProp
Props allows you to re-use the same component for different datasets. In the example above, we are able to re-use the Turtle component for all four ninja turtles because the component will show different info based on what data we pass in!
JSS Recap
<div style={{ backgroundColor: 'red' }} />
When you are using inline styles, you need to pass an object for the style attribute.
Because you are passing an object, you will need to open a set of {} for the jsx expression AND another set for the actual object literal.
Alternatively you could do the following for readibility
<form>
<button type="button">Click me to NOT submit the form</button>
<button type="submit">Click me and the form WILL submit</button>
</form>
When working with buttons inside of a form tag, you must specify the type attribute. A button without a type attribute will be treated as a submit button by default and clicking on it will submit your form.
Mapping over an array is the equivalent of hardcoding these components. As you can see, it can get out of hand very quickly. Imagine an array of over 10,000 elements!
The shorthand ... operator, allows you to pass all the data in an object down as props to a child component. All the keys on the object, will become the name of each prop. All the values on the object, will become the value on each of the corresponding prop. In the example above, ExampleComponent now has access to props.name, props.age, and props.occupation. The values will be 'Andy', 12, and 'Software Engineer respectively
useEffect
useEffect(EFFECT_CALLBACK, DEPENDENCIES)
useEffect(() => {
console.log('foo has changed!')
}, [foo])
useEffect is a react hook, that allows you perform side-effects. These effects can occur for three reasons
(1) When the component mounts, this means when the component is rendered for the very first time.
(2) When the component unmounts, this means when the componeent leaves the DOM
(3) When any of the dependencies changes, meaning one of the values in the array of dependencies gets modified either because of new props or new state.
When the useEffect determine that the DEPENDENCIES (argument 2) has been updated, it will then run the EFFECT_CALLBACK (argument 1).
This allows you to perform lifecycle method, one example is fetching data when the component is first rendered.
In the App component, you can see we have a useEffect that will call fetchTurtles
The dependencies array for this effect is empty, so this means that the effect will run once and only once
When you pass an empty dependencies array, React has nothing to hook onto, therefore the effect will never run again (reminder that the purpose of the array is if you want React to re-run the effect when certain variables change)
Callouts for useEffect
const [someState, setSomeState] = useState(0);
// infinite loop - This `effect` will update the `state` of `someState` which is also the dependency for the `effect`
useEffect(() => {
setSomeState(someState + 1)
}, [someState])
// no infinite loop - This `effect` will only run update `state` if `someState` meets a specific condition
useEffect(() => {
if (someState === 0) {
setSomeState(100)
}
}, [someState])
It is very easy to land yourself in an infinite loop with useEffect.
You should never set the state of one of a dependency inside the callback without any conditionals. Similar to how you would never write a loop or recursive function without a break condition
The above is an example of everything you've learned to this point. Refer to the ref-$n for each of the following descriptions
ref-1 is an example of declaring state variables.
These are the two variables that we know for certain will change during runtime.
games starts off as an empty array and will eventually populate when we call out api.
searchTerm is an empty string initially but will change as the user types in the input
ref-2 is an async function that will call the games api, then setState of games with the response.
The function also interpolates the searchTerm as a query param so if the user types a particular type of games, the filter will be applied to the fetch
The response will be an array of games [{ id: 'game-1', title: 'Mario Kart' }, ...moreGames].
This will cause a re-render!
ref-3 is a useEffect with an empty dependencies array
This effect will run once when the component first mounts the DOM. It will never run again because there are no dependencies in the array meaning there is nothing to hook onto.
The callback function for this effect simply calls fetchGames the function detailed by ref-2
ref-4 is an event handler for the input tag
This handler calls setSearchTerm anytime the user types in the input tag.
This will cause a re-render!
ref-5 is an event handler for when the form submits.
This handler prevents the default behavior of an html5 form tag
It also calls fetchGames to update the list of games according to what the user has typed in, which has now been stored in the state variable searchTerm due to ref-4
ref-6 The JSX that is rendered by the App component
The form tag has an onSubmit event listener attached that will call the handleFormSubmit event handler detailed by ref-5
The input tag has an onChange event listener attached that will call the handleInputChange event handler detailed by ref-4
Under the form, the games array gets mapped, and will render a Game component for each game in the array, passing a key and title as props to the component.
08/25 Day 21 Lecture Notes
Props Recap
<Turtle someProp="some value">
props.someProp
Turtle
component for all four ninja turtles because the component will show different info based on what data we pass in!JSS Recap
object
for thestyle
attribute.object
, you will need to open a set of{}
for the jsx expression AND another set for the actual object literal.Buttons and Forms
form
tag, you must specify thetype
attribute. A button without atype
attribute will be treated as asubmit
button by default and clicking on it will submit your form.Rendering a collection (mapping over an array)
String
,Array
, andJSX.element
. Anything else will either be coerced or ignored.map
returns anarray
therefore when youmap
over an array in thereturn
of a component, you are rendering anarray
.map
s over an array ofturtles
, thenreturn
s aTurtle
component for each item in the array.<Component {...item} />
...
operator, allows you to pass all the data in an object down as props to a child component. All the keys on the object, will become the name of each prop. All the values on the object, will become the value on each of the corresponding prop. In the example above,ExampleComponent
now has access toprops.name
,props.age
, andprops.occupation
. The values will be'Andy'
,12
, and'Software Engineer
respectivelyuseEffect
useEffect
is a react hook, that allows you perform side-effects. These effects can occur for three reasonsprops
or newstate
.useEffect
determine that theDEPENDENCIES
(argument 2) has been updated, it will then run theEFFECT_CALLBACK
(argument 1).App
component, you can see we have auseEffect
that will callfetchTurtles
dependencies
array for this effect is empty, so this means that the effect will run once and only oncedependencies
array, React has nothing to hook onto, therefore the effect will never run again (reminder that the purpose of the array is if you want React to re-run the effect when certain variables change)Callouts for
useEffect
useEffect
.Putting it all together
ref-$n
for each of the following descriptionsref-1
is an example of declaringstate
variables.games
starts off as an empty array and will eventually populate when we call out api.searchTerm
is an empty string initially but will change as the user types in the inputref-2
is an async function that will call the games api, thensetState
ofgames
with the response.searchTerm
as a query param so if the user types a particular type of games, the filter will be applied to thefetch
[{ id: 'game-1', title: 'Mario Kart' }, ...moreGames]
.ref-3
is auseEffect
with an empty dependencies arrayfetchGames
the function detailed byref-2
ref-4
is an event handler for theinput
tagsetSearchTerm
anytime the user types in the input tag.ref-5
is an event handler for when the form submits.form
tagfetchGames
to update the list of games according to what the user has typed in, which has now been stored in thestate
variablesearchTerm
due toref-4
ref-6
TheJSX
that is rendered by theApp
componentform
tag has anonSubmit
event listener attached that will call thehandleFormSubmit
event handler detailed byref-5
input
tag has anonChange
event listener attached that will call thehandleInputChange
event handler detailed byref-4
games
array gets mapped, and will render aGame
component for each game in the array, passing akey
andtitle
as props to the component.