r/reactjs • • Feb 01 '19

Needs Help Beginner's Thread / Easy Questions (February 2019)

🎊 This month we celebrate the official release of Hooks! 🎊

New month, new thread 😎 - January 2019 and December 2018 here.

Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch.

No question is too simple. πŸ€”

Last month this thread reached over 500 comments! Thank you all for contributing questions and answers! Keep em coming.


πŸ†˜ Want Help with your Code? πŸ†˜

  • Improve your chances by putting a minimal example to either JSFiddle or Code Sandbox. Describe what you want it to do, and things you've tried. Don't just post big blocks of code!
  • Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.

Have a question regarding code / repository organization?

It's most likely answered within this tweet.


New to React?

πŸ†“ Here are great, free resources! πŸ†“


Any ideas/suggestions to improve this thread - feel free to comment here or ping /u/timmonsjg :)

35 Upvotes

484 comments sorted by

View all comments

Show parent comment

1

u/Kazcandra Feb 02 '19

First of all, what would JS evaluate <br /> to? Second, you should read the Date object documentation, because you're doing a /lot/ of unnecessary work here. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

But I guess you want something like this (untested)?

function AbbreviatedMonths(date) {
  const month = date.toLocaleString('en-us', { month: 'short' });
  const year = date.getFullYear();
  return( 
    <div>
      {month}
      {month === 'Jan' ? `<br />${year}` : ''}
    </div>
  )
}

1

u/seands Feb 02 '19

will the <br/> tag render from a string template? if so that looks good

1

u/Kazcandra Feb 02 '19

Dunno、I wouldn't solve it like that personally. That's why I said untested.

If it doesn't 、you could always make the logic better.