DEV Community

Rishikesh-Programmer
Rishikesh-Programmer

Posted on

Calender in react js

In this post we are going to make a simple calender in react js

run the the following commands in the terminal:

npx create-react-app react-calender
cd react-calender
npm i react-calender
Enter fullscreen mode Exit fullscreen mode

go to src/app.js and erase the content in it,
and import react, react-calender

import React from 'react';
import Calendar from 'react-calendar';
import 'react-calendar/dist/Calendar.css';
Enter fullscreen mode Exit fullscreen mode

next, create a function app and export

export default function App() {
  // main content goes here...
}
Enter fullscreen mode Exit fullscreen mode

and set a value and onchange using react useState

export default function App() {
  const [value, onChange] = React.useState(new Date());
}
Enter fullscreen mode Exit fullscreen mode

and then

export default function App() {
  const [value, onChange] = React.useState(new Date());

  return (
    <div>
      <Calendar onChange={onChange} value={value} />
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

next go to src/index.js and paste the following code:

import React, { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';

import App from './App';

const rootElement = document.getElementById('root');
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <App />
  </StrictMode>
);

Enter fullscreen mode Exit fullscreen mode

app.js full source code:

import React from 'react';
import Calendar from 'react-calendar';
import 'react-calendar/dist/Calendar.css';

export default function App() {
  const [value, onChange] = React.useState(new Date());

  return (
    <div>
      <Calendar onChange={onChange} value={value} />
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

live demo: https://reactcalenderbyrishi.stackblitz.io/

thankyou

Top comments (0)