React Drills
Main Youtube Playlist - 5-15 Min Videos: https://www.youtube.com/playlist?list=PLXqOQRPHA5ltU2uU0DzcQ4PsGPLfw7k8F
Longer Playlist - 20+ Min Videos: https://www.youtube.com/playlist?list=PLXqOQRPHA5lvKBQ2AIjAcS1DuQKMHhE7t
Web Dev Simplified - Branches ws_#_desc
- Git: https://github.com/WebDevSimplified
- Vid: https://www.youtube.com/@WebDevSimplified
- Blog:
- Solutions: in App.jsx or in README.md
To install: npm i
To run: npm run dev
Drills
1 Props - Go to 'src/drills/props' to the 'Props.jsx'. For the App.jsx, import 'Props' and 'Parent' Reference: https://www.youtube.com/watch?v=Rh3tobg7hEo
- Pass a prop 'firstName' from the Parent component to the child component
- The same as 1 to Child2 but now pass a last name and use JS Destructing.
- Now pass a first & last name and a function to Child3. Use JS Destructing.
- For Child4, pass a boolean and use it
2 Components - Go to 'src/drills/components'. For the App.jsx, import your components Reference: https://youtu.be/hn80mWvP-9g?si=sHHWUOdIYduhRZ1o
- Create a 'Header.jsx' component. Put in elements such as header, h1 and any other elements such as nav, ul, li, hr, etc
- Create a 'Food.jsx' component. Put in an ordered or unordered list with your favorite food.
- Create a 'Footer.jsx' component. Put in a footer element with content enclosed in a p element
- Add them all in the App.jsx
3 Conditional Rendering - Go to 'src/drills/render'. For App.jsx, import your components Reference: https://www.youtube.com/watch?v=XvURBpFxdGw
-
Create a 'UserGreeting.jsx' component.
-
In the App.jsx, pass the props 'isLoggedIn' with a boolean of true and 'userName' with any string.
-
In the 'UserGreeting.jsx' use if else to show a message if the user is logged in. Ex. "Welcome Mike" or "Please log in"
-
Try it with a teneary operator
4 Events Reference: https://www.youtube.com/watch?v=KpiiKuqNlYw&t=264s
-
Part 1 - Function w no params for an event Create a 'Button.jsx' and add a button element with
-
Add an onClick event
-
Add a handleClick() method to alert "OUCH".
-
Part 2 - Function w parameter for an event Now add a parameter with name on 'handleClick2(name)' to a 2nd button.
-
Notice it will auto run the function. To prevent that, add an arrow function to the onClick event to prevent it.
-
Part 3 - Function with conditionAdd a count =0
-
On the 'handleClick3', have 1 parameter of name.
-
Add a condition, if count less than 3, add to the count and alert ", You clicked me of times"
-
Else alert ", stop clicking me"
-
Part 4 - The Event parameter Now create 'handleClick4' with an 'event' argument. Use 'e'
-
Console.log the event.
-
Make sure to wrap it with an arrow function in the event. Ex. {(e)=> handleClick4(e)}
-
Now lets change the text content from 'Click Me' to 'Ouch' using 'e.target.textContent = "Ouch"'
-
Part 5 - Doubleclick Change from 'onClick' to 'doubleClick' for 'handleClick4'
-
Part 6 - Picture w event Create a 'ProfilePicture.jsx'
-
Make sure you have an image in 'assets' and import it to this jsx.
-
Create an 'imageUrl' that is = to the image in the right directory
-
Create an 'img' element with src={imageUrl}
-
Add an onClick for a handleClick function that alerts 'OUCH'.
-
Part 7 - Picture w event passing the event That should work. Now create a new event 'handleClick2' that has a parameter of the event as 'e' to the 'handleClick2' function.
-
Since you have the event, just use the event to hide the image Ex. e.target.style.display = "none"
5 Invalid Hook Call
- Git:
- Video: https://www.youtube.com/watch?v=kf8loX8AN-c
- There are 3 ways to make the hook invalid. Try to initlize it outside of the functional component body, inside a useEffect hook and inside an onClick handler function. You can access and set it but not initialized it.
6 Import
- Git:
- Videos: 1min+ https://www.youtube.com/watch?v=uGz8YNZNnx8&list=PLXqOQRPHA5ltU2uU0DzcQ4PsGPLfw7k8F&index=36&pp=gAQBiAQB
- Create a child comp under '/drills/components', export it and use it with an import.
- Create a js file with multiple functions to be used under '/utils/, export it and use it with an import
7 Conditional Rendering
-
Git:
-
Videos/Article: https://chorezap.blogspot.com/2024/02/react-conditional-rendering-4-ways.html
-
Do it 3 ways on App.jsx