Responsive Notifications Page Using React, CSS Fflexbox, CSS Grid
Design comparison
Solution retrospective
I just learned how to use the useState
, but I wasn't sure if I had used it wisely.
Questions:
- If I have imported a data list (notificationsList.js in this case) in the App.js, can I just pass it on as a prop or is it better to import it on the component? Does having the
useState
in the App.js as the parent affect this decision?
I hope someone can review my code which can be found here and let me know how I can improve on using it.
Thank you so much! :)
Community feedback
- @tan911Posted almost 2 years ago
It's okay to import 'notificationList' directly to the component's who needs it. Also you can pass 'notificationList' as props. However when passing it as props I think it's read not a notificationList, the code looks like this,
import Header from './components/header/Header'; import Notifications from './components/notifications/Notifications'; import notificationsList from './data/notificationsList'; export default function App() { const [read, setRead] = useState(notificationsList); return ( <main className="main"> <section className="notifications"> <div className="c-notifications"> <Header setRead={setRead} notificationsList={read} /> <Notifications /> </div> </section> </main> ); }
You use 'useState' with initial value of 'notificationsList'. You don't need to pass 'notificationList' itself instead use the 'read' variable to pass data into your children component.
Marked as helpful1@ereljapcoPosted almost 2 years agoOh, right. Forgot about the initial value. Thank you, @tan911! ☺ By the way, can I ask which is the best practice in this case? Passing the initial value as props or importing it on the component?
0@tan911Posted almost 2 years ago@ereljapco Welcome, Importing on the component is even better.
1@ereljapcoPosted almost 2 years ago@tan911 Thank you so much! I will refactor my code using
import
then ☺1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord