DEV Community

Cover image for ๐Ÿ• Eat-N-Split Day 2: Adding Friends & Toggling the Form
Usama
Usama

Posted on

๐Ÿ• Eat-N-Split Day 2: Adding Friends & Toggling the Form

Today I continued my Eat-N-Split course project in React โ€” and it was a fun one!
I worked on opening/closing the Add Friend form and rendering newly added friends dynamically.


๐ŸŽฏ What I Focused On Today

The main goal of Day 2 was:

  1. Show and hide the โ€œAdd Friendโ€ form when the user clicks a button.
  2. Add a new friend to the list and update the UI instantly.

This is a perfect example of React state in action.


๐Ÿงฉ Opening & Closing the Form

I used a simple boolean state to toggle the form visibility:

const [friendFormIsOpen, setFriendFormIsOpen] = useState(false);
Enter fullscreen mode Exit fullscreen mode

And a button that flips it:

<Button onClick={() => setFriendFormIsOpen(show => !show)}>
  {friendFormIsOpen ? "Close" : "Add Friend"}
</Button>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Fun thing: This tiny little line of code lets you open and close the form with one button. No rocket science, just React magic. โœจ


๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ Adding a Friend

When a new friend is submitted:

  • A new friend object is created with a unique ID, name, image, and a default balance.
  • Then I update the friends state:
setFriends(friends => [...friends, newFriend]);
Enter fullscreen mode Exit fullscreen mode

The cool part? React instantly re-renders the UI with the new friend without refreshing the page. ๐Ÿช„


๐Ÿ˜‚ What I Learned Today (With a Smile)

  • useState is your best friend โ€” literally!
  • Toggling things with a boolean is satisfying. Flip โ†’ show, Flip โ†’ hide.
  • Adding items to an array in React is as simple as spreading the old array and adding the new item.
  • Watching the new friend appear on the list feels like magic every time. โœจ

๐Ÿ”ฎ Next Steps

Tomorrow (or next session), Iโ€™ll focus on:

  • Splitting a bill with a selected friend
  • Handling the calculation of balances dynamically
  • Making the app fully interactive

Top comments (0)