r/reactjs • • Jun 01 '21

Needs Help Beginner's Thread / Easy Questions (June 2021)

Previous Beginner's Threads can be found in the wiki.

Ask about React or anything else in its ecosystem :)

Stuck making progress on your app, need a feedback?
Still Ask away! We’re a friendly bunch πŸ™‚


Help us to help you better

  1. Improve your chances of reply by

    1. adding a minimal example with JSFiddle, CodeSandbox, or Stackblitz links
    2. describing what you want it to do (ask yourself if it's an XY problem)
    3. things you've tried. (Don't just post big blocks of code!)
  2. Format code for legibility.
  3. Pay it forward by answering questions even if there is already an answer. Other perspectives can be helpful to beginners. Also, there's no quicker way to learn than being wrong on the Internet.

New to React?

Check out the sub's sidebar! πŸ‘‰
For rules and free resources~

Comment here for any ideas/suggestions to improve this thread

Thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!


20 Upvotes

305 comments sorted by

View all comments

Show parent comment

1

u/dshkodder Jun 06 '21 edited Jun 06 '21

Thank you! I managed to render my data to calendar cellsLink to my solution

I faced another issue though. I want to render the whole calendar cell as a link to a workout page rather than just some text inside the cell if it matches the date from my data because I want to make aiming to click on the workout day easier. Therefore, I decided to add a condition in the return statement My attempt(full code):

  return workouts.map((i) => {
return new Date(i.date).getDate() === date &&
  new Date(i.date).getMonth() === month &&
  new Date(i.date).getFullYear() === year ? (
  <Link to={`${i._id}`}>
    <div className="calendar__cell">
      <div className="calendar__cell__date">{date}</div>
      <p>WD</p>
    </div>
  </Link>
) : (
  <div className="calendar__cell">
    <div className="calendar__cell__date">{date}</div>
  </div>
);

}

Now it returns everything in one cell - what I need and what I don't. I guess I iterate over the array somehow wrong. How can I render conditionally the whole cell as a link when it matches the date from the given data and just a day number if it doesn't? Instead of rendering everything at once...

Edit: Here is the solution https://codesandbox.io/s/workout-calendar-multiple-events-08b8s?file=/src/App.js thanks to Michael Shields for it.