Techstacker
Light ModeDark ModeReset dark mode. Falls back to OS dark mode setting.

Pages

No results for 'undefined'

Blog Posts

No results for 'undefined'
Powered by Algolia

How to Show the Current Year With React

Sep 24, 20181 min readReactViews:

In this quick lesson you’ll learn how to show the current year with React.js.

In a previous lesson, you learned how to show the current year with vanilla JavaScript. With React, it's almost the same. Simply add the following code inside your render function, like this:

class ShowCurrentYear extends React.Component {
    render() {
        return 
{(new Date().getFullYear())}
; } }

You could also create the get current year function outside of your render function, like this:

showCurrentYear() {
    return new Date().getFullYear();
}

And then insert the output inside your render function wherever you want to show the current year in your app, like this:

<div>
    {this.showCurrentYear()}
</div>

Like with anything JavaScript, there are other ways of doing this. If you know a better/different approach, let me know in the comments section down below!