React Lessons I leaned tonight
I was studying React tonight and learned:
  1. You include all pages and components that are supposed to go on the websites, and the components and webpages you want to show on every page the user visits, you put these components and webpages outside of the routes on app.js, but the components and webpages you don't want to show on every page , you put these webpages and components in routes on the app.js file.
  2. You use a context provider to wrap all of the route, routes router and wrap everything in the context provider, so you can share data with all the components inside the context provider in the function app.
  3. The purpose of context providers is they allow you to share date with the child components that are inside the context providers without using prop drilling.
  4. Some components don't need to use the context provider in their own individual file while some components need to use the context provider in their own file because some components need access to the shared data . An example of this : import React from 'react';
  5. import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
  6. import UserContext from './UserContext';
  7. import Header from './Header';
  8. import Profile from './Profile';
  9. import Home from './Home';
  10. import About from './About';
  11. function App() {
  12. const user = { name: 'Alice', loggedIn: true };
  13. return (
  14. <UserContext.Provider value={user}>
  15. <Router>
  16. <Header /> {/* This will show on every page */}
  17. <Routes>
  18. <Route path="/" element={<Home />} />
  19. <Route path="/profile" element={<Profile />} />
  20. <Route path="/about" element={<About />} />
  21. </Routes>
  22. </Router>
  23. </UserContext.Provider>
  24. );
  25. }
  26. export default App; import React from 'react';
  27. function Home() {
  28. return <div>Home Page</div>;
  29. }
  30. export default Home;import React from 'react';
  31. function About() {
  32. return <div>About Page</div>;
  33. }
  34. export default About;import React, { useContext } from 'react';
  35. import UserContext from './UserContext';
  36. function Profile() {
  37. const user = useContext(UserContext);
  38. return (
  39. <div>
  40. <h2>Profile</h2>
  41. <p>Name: {user.name}</p>
  42. <p>Status: {user.loggedIn ? 'Logged In' : 'Logged Out'}</p>
  43. </div>
  44. );
  45. }
  46. export default Profile;import React, { useContext } from 'react';
  47. import UserContext from './UserContext';
  48. function Header() {
  49. const user = useContext(UserContext);
  50. return <header>Welcome, {user.name}!</header>;
  51. }
  52. export default Header;
  53. Prop drilling is when you pass props to multiple components.
These are the things I learned tonight as I am studying React. Hayk, is this correct what I learned or did I get something wrong. I don't know why it shows numbers like this, but from line 5 to 55 is the example of lesson 4.
1
2 comments
SeaAmber Silver
6
React Lessons I leaned tonight
Dev Mastery
skool.com/devmastery
Break out the mid-tier developer trap. Get to Senior, fast.
Leaderboard (30-day)
Powered by