is this called as prop drilling ?

// App.js
import { useState } from 'react';
import Rating from './components/Rating';
import Thankyou from './components/Thankyou';

function App() {
const [selectedRating, setSelectedRating] = useState(0);
const [showThankyou, setShowThankyou] = useState(false);

const handleRatingChange = (rating) => {
setSelectedRating(rating);
};

const handleRatingSubmit = () => {
setShowThankyou(true);
};

return (
<div className='font-outfit h-screen grid place-items-center'>
{!showThankyou ? (
<Rating onRatingChange={handleRatingChange} onRatingSubmit={handleRatingSubmit} />
) : (
<Thankyou value={selectedRating} />
)}
</div>
);
}

export default App;
// App.js
import { useState } from 'react';
import Rating from './components/Rating';
import Thankyou from './components/Thankyou';

function App() {
const [selectedRating, setSelectedRating] = useState(0);
const [showThankyou, setShowThankyou] = useState(false);

const handleRatingChange = (rating) => {
setSelectedRating(rating);
};

const handleRatingSubmit = () => {
setShowThankyou(true);
};

return (
<div className='font-outfit h-screen grid place-items-center'>
{!showThankyou ? (
<Rating onRatingChange={handleRatingChange} onRatingSubmit={handleRatingSubmit} />
) : (
<Thankyou value={selectedRating} />
)}
</div>
);
}

export default App;
49 Replies
indee
indeeOP17mo ago
// Rating.js
import { useState } from "react";

export default function Rating({ onRatingChange, onRatingSubmit }) {
const [active, setActive] = useState(null); // Set the initial active state to null

const handleRatingSelect = (rating) => {
onRatingChange(rating);
setActive(rating); // Set the active state to the selected rating
};

return (
<div className='border-2 border-black w-96 p-4'>
<p className="text-5xl">How did we do?</p>
<p>Please let us know how we did with your support request. All feedback is appreciated to help us improve our offering!</p>
<div className='flex flex-row gap-5'>
{[1, 2, 3, 4, 5].map((rating) => (
<p
className={`w-12 h-12 rounded-full flex items-center justify-center ${ active === rating ? "bg-blue-500" : "bg-blue-100" }`}
key={rating}
onClick={() => handleRatingSelect(rating)}
>
{rating}
</p>
))}
</div>
<button className='w-full mt-4' onClick={onRatingSubmit}>
submit
</button>
</div>
);
}
// Rating.js
import { useState } from "react";

export default function Rating({ onRatingChange, onRatingSubmit }) {
const [active, setActive] = useState(null); // Set the initial active state to null

const handleRatingSelect = (rating) => {
onRatingChange(rating);
setActive(rating); // Set the active state to the selected rating
};

return (
<div className='border-2 border-black w-96 p-4'>
<p className="text-5xl">How did we do?</p>
<p>Please let us know how we did with your support request. All feedback is appreciated to help us improve our offering!</p>
<div className='flex flex-row gap-5'>
{[1, 2, 3, 4, 5].map((rating) => (
<p
className={`w-12 h-12 rounded-full flex items-center justify-center ${ active === rating ? "bg-blue-500" : "bg-blue-100" }`}
key={rating}
onClick={() => handleRatingSelect(rating)}
>
{rating}
</p>
))}
</div>
<button className='w-full mt-4' onClick={onRatingSubmit}>
submit
</button>
</div>
);
}
export default function Thankyou ({value}) {
return (
<div className='border-2 border-black w-96 p-4'>
<p>you selected {value} out of 5</p>
<h2>Thank you</h2>
<p>We appreciate you taking the time to give a rating. If you ever need more support, don’t hesitate to get in touch!</p>
</div>
);
}
export default function Thankyou ({value}) {
return (
<div className='border-2 border-black w-96 p-4'>
<p>you selected {value} out of 5</p>
<h2>Thank you</h2>
<p>We appreciate you taking the time to give a rating. If you ever need more support, don’t hesitate to get in touch!</p>
</div>
);
}
how to prevent it guys ?
b1mind
b1mind17mo ago
That just looks like your passing props? Prop drilling is when you have one component pass it a prop, its got a child you pass it the same prop, then its got a child component too and you pass it the same prop hence "drilling"
indee
indeeOP17mo ago
so if three times its called prop drilling ?
b1mind
b1mind17mo ago
I mean even 2 times is but one time is just passing a prop xD
indee
indeeOP17mo ago
i heard that passing that statesetter through a prop it self a prop drilling
b1mind
b1mind17mo ago
App > Title > Welcome = all got prop hello from App that would be drilling
indee
indeeOP17mo ago
oh ok
b1mind
b1mind17mo ago
App passes to Title > drills to Welcome if that makes sense xD
indee
indeeOP17mo ago
so what am i doing is it good to do ?
b1mind
b1mind17mo ago
I mean I don't use React so probably not best to say if that code is good or not >.>;; but yes just passing props is a normal thing drilling is rather normal practice too if not using global store
indee
indeeOP17mo ago
heard that its a bad practice
b1mind
b1mind17mo ago
best and bad practices are a lie more like guidelines you figure out whats best/bad for your situation.
indee
indeeOP17mo ago
okay
b1mind
b1mind17mo ago
I think its bad in React if you are going too deep you should probably question how you are doing things xD like 2 or 3 🤷‍♂️ ... 5 or 6... yea probably not good
indee
indeeOP17mo ago
ig i would just use something like a link or useNavigate or something
b1mind
b1mind17mo ago
Your only other option in React is to use Context API or redux ?
indee
indeeOP17mo ago
will that be fine ?
b1mind
b1mind17mo ago
idk again thats React land, I don't dabble with it Svelte(Kit) I'm leveraging how the browser works and not having to think about half that crap so and it has Stores ❤️
indee
indeeOP17mo ago
react vs svelte which is better ? i am a beginner
b1mind
b1mind17mo ago
I'm biased so ... Svelte is much easier and nice output xD but you will hear the bs like "react if you want a job" blah blah
indee
indeeOP17mo ago
what you started with ?
b1mind
b1mind17mo ago
Svelte is becoming more popular and will shift the JS world over time, its already adopted by some big players and Vercels got Rich on payroll just to work full time on it so that is a pretty big bet being they own NextJS fully I pretty much started with Svelte xD (but I do look at how others do things a little) I chose it knowing what I wanted from a UI framework though 😉
indee
indeeOP17mo ago
i just heard it is what ppl are using cuz they say it a standard and has a lot of community and libgs i mean react so i started using it
b1mind
b1mind17mo ago
Reacts the popular one yup 😄
indee
indeeOP17mo ago
i have seen svelte
b1mind
b1mind17mo ago
also a PITA lol
indee
indeeOP17mo ago
looks like astro
b1mind
b1mind17mo ago
Astro took lots of inspiration from SvelteKit as Fred worked with the team when he was doing Snowpack. Then Kit ditched snowpack for Vite and Fred made Astro 😄 (and also ended up switching to Vite from snowpack with Astro hehe) Also html template languages so none that JSX garbo
indee
indeeOP17mo ago
vite is fast man like zuuuuuuuuuuuuuuuum
b1mind
b1mind17mo ago
Yea snowpack was legit but even Fred succeeded to Vite xD We would not have Astro or Kit for what it is without Vite heh
indee
indeeOP17mo ago
i know webpack ( crm ) never heard of snow pack
b1mind
b1mind17mo ago
both are Vite plugins basically (well Kit is now they fully abstracted it, so it plays nice with other Vite plugins) Fred Scott the author of Astro made snowpack and skypack It was really nice too pretty much the same idea as Vite, but Evan You is just a mad lad and knocked it out the park lol It was actually better in some ways cause Vite only did single entry points and snowpack would look for any (so worked well with other template langs)
indee
indeeOP17mo ago
thats a story man, where you heard it ?
b1mind
b1mind17mo ago
I pay attention lol I've been using SvelteKit since its birth and the rest of the tools around it
indee
indeeOP17mo ago
oh
b1mind
b1mind17mo ago
I adopted Snowpack when they did and Fred was helping with it (pre Astro days) and watched all that go down as it happened 🤣
indee
indeeOP17mo ago
have a lot of time using just svelte ha ? you use it at your job ?
b1mind
b1mind17mo ago
11ty scene too cause its neat... lots of time ya, kinda I freelance but only ahve had the chance to use it in one website so far. but yea /end long rant
indee
indeeOP17mo ago
i tried hugo they say its fast but not a lot of flexibility
b1mind
b1mind17mo ago
way off topic lol
indee
indeeOP17mo ago
you said 11ty its a ssg i am using astro now
b1mind
b1mind17mo ago
Know though the idea of Prop drilling is not just React so concept is the same for them all 11ty is a ssg yea only a SSG though no SSR like Astro
indee
indeeOP17mo ago
ohh
b1mind
b1mind17mo ago
Astro is becoming even more like SvelteKit soon too they just released the Client side router for it #transitionalApps So you can have the best of all worlds ❤️
indee
indeeOP17mo ago
hmm didnt understand much but astro i will use it
b1mind
b1mind17mo ago
back on topic though, what you want with React is to look up Prop Drilling vs Context API find all the spicy debates about it and make up your own choice if you see a perf issue or have issues with the abstractions getting out of hand.
indee
indeeOP17mo ago
i need to use context api someday
b1mind
b1mind17mo ago
probably idk people also speak up against using it xD freaking React mang.. 🤣
indee
indeeOP17mo ago
then what do i have use man hehehehe i heard that too man react heheheh lets see i am giving the time to do its thing where it says me just leave it then ill do it
Want results from more Discord servers?
Add your server