React Native course · Module 13: Native APIs
expo-calendar - Calendar and events
In this lesson5
Integrate the app with the user's calendar!
Installation
npx expo install expo-calendarFetching calendars
import * as Calendar from 'expo-calendar';
async function getCalendars() {
const { status } = await Calendar.requestCalendarPermissionsAsync();
if (status !== 'granted') {
Alert.alert('No calendar permissions');
return [];
}
const calendars = await Calendar.getCalendarsAsync(Calendar.EntityTypes.EVENT);
return calendars;
}Fetching events
async function getEvents(calendarId: string, startDate: Date, endDate: Date) {
const events = await Calendar.getEventsAsync(
[calendarId],
startDate,
endDate
);
return events;
}
// Example: events for the next 7 days
const now = new Date();
const nextWeek = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
const events = await getEvents('calendar-id', now, nextWeek);Creating an event
async function createEvent(
calendarId: string,
title: string,
startDate: Date,
endDate: Date,
location?: string
) {
const eventId = await Calendar.createEventAsync(calendarId, {
title,
startDate,
endDate,
location,
notes: 'Created by Atlantis',
alarms: [{ relativeOffset: -30 }], // Reminder 30 min before
});
return eventId;
}Creating a calendar
async function createCalendar() {
const defaultCalendarSource = Platform.OS === 'ios'
? await Calendar.getDefaultCalendarAsync()
: { isLocalAccount: true, name: 'Expo Calendar' };
const calendarId = await Calendar.createCalendarAsync({
title: 'Atlantis Events',
color: '#1a5276',
entityType: Calendar.EntityTypes.EVENT,
sourceId: defaultCalendarSource.id,
source: defaultCalendarSource,
name: 'atlantisCalendar',
ownerAccount: 'personal',
accessLevel: Calendar.CalendarAccessLevel.OWNER,
});
return calendarId;
}Code for this lesson: App.tsx
import React, { useState, useEffect } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
import * as Calendar from 'expo-calendar';
// Hook for handling the user's calendar
function useCalendar() {
const [events, setEvents] = useState<Calendar.Event[]>([]);
const [calendarId, setCalendarId] = useState<string | null>(null);
useEffect(() => {
(async () => {
// TODO: request calendar permissions:
// const { status } = await Calendar.requestCalendarPermissionsAsync();
// if (status !== 'granted') return;
// TODO: fetch event calendars:
// const calendars = await Calendar.getCalendarsAsync(Calendar.EntityTypes.EVENT);
// if (calendars.length > 0) setCalendarId(calendars[0].id);
})();
}, []);
async function loadEvents() {
if (!calendarId) return;
const now = new Date();
const nextWeek = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
// TODO: fetch events from the next week:
// const data = await Calendar.getEventsAsync([calendarId], now, nextWeek);
// setEvents(data);
}
return { events, calendarId, loadEvents };
}
export default function App() {
const { events, loadEvents } = useCalendar();
return (
<View style={styles.screen}>
<Text style={styles.title}>Atlantis Calendar</Text>
<Button title="Load events" onPress={loadEvents} />
{events.map((event) => (
<Text key={event.id} style={styles.item}>{event.title}</Text>
))}
</View>
);
}
const styles = StyleSheet.create({
screen: {
flex: 1,
padding: 24,
backgroundColor: '#1a5276',
},
title: {
fontSize: 22,
color: '#fff',
fontWeight: '600',
marginBottom: 16,
},
item: {
fontSize: 16,
color: '#d6eaf8',
paddingVertical: 6,
},
});Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. How do you install expo-calendar?
2. How do you get the list of calendars?
These are 2 of 4 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Click in order
Click the elements for fetching events:
- Code editor
Complete the useCalendar code.