How to create custom calendar in React Native?
There are couple of ways to make your custom calendar
You can download libraries[not install] and then edit their code to make your own library and add it to your component.(preferred way)
Or you can make your layout for days, weeks , months, etc and use scroll-view to make it swipe-able. Most of libraries also uses scroll-view.
Creating a calendar component in react native
You should set the state like below
function changeMonth(n) {
setState({
activeDate: new Date(state.activeDate.setMonth(state.activeDate.getMonth() + n))
});
}
When you do it using the current way you are setting a callback function which does not have a getFullYear function.
react-native-calendar : apply custom multi marking with popup
You need to add the marked date to your _markedDates
object. On day press show modal and when the user clicks x
save the marked date, with selected types in dots array.
state = {
_markedDates: this.initialState,
isOpen: false,
isDisabledOne: false,
isDisabledTwo: false,
isDisabledThree: false,
isOpen: false,
selectedDay: ''
};
saveDay = () => {
const dots = [];
let selected = true;
const {
isDisabledOne,
isDisabledTwo,
isDisabledThree,
_markedDates,
selectedDay
} = this.state;
if (isDisabledOne) {
dots.push(vacation);
}
if (isDisabledTwo) {
dots.push(massage);
}
if (isDisabledThree) {
dots.push(workout);
}
if (_markedDates[selectedDay]) {
selected = !_markedDates[selectedDay].selected;
}
const clone = { ..._markedDates };
clone[selectedDay] = { dots, selected };
this.setState({
isOpen: false,
_markedDates: clone,
isDisabledOne: false,
isDisabledTwo: false,
isDisabledThree: false,
});
};
const BContent = () => (
<Button
title="X"
onPress={this.saveDay}
style={[styles.btn, styles.btnModal]}
/>
);
onDaySelect
onDaySelect = day => {
const _selectedDay = moment(day.dateString).format(_format);
this.setState({
selectedDay: _selectedDay,
isOpen: true
});
};
DEMO
react-native-calendars jump to a selected month
Solved this issue by adding both props key
and current
Prop key
triggers re-rendering
const CalendarScreen = () => {
const [current, setCurrent] = useState('2020-01-01');
return(
<Calendar
current={current}
key={current}
customHeader={CalendarHeaderHandler}
style={styles.calendar}
/>
<TouchableOpacity onPress={() => setCurrent('2021-01-01')}>
<Text>2021</Text>
</TouchableOpacity>
)};
Render image on each date using react-native-calendars
As per the doc you could use dayComponent
property to override the component
.
<Calendar
style={[styles.calendar, {height: 300}]}
dayComponent={({date, state}) => {
return (
<View>
<Text style={{textAlign: 'center', color: state === 'disabled' ? 'gray' : 'black'}}>
{date.day}
</Text>
</View>
);
}}
/>
Related Topics
How to Display 3 Cells Per Row in Uicollectionview
Transport Security Has Blocked a Cleartext Http
How to Set Up a Simple Delegate to Communicate Between Two View Controllers
Instantiate and Present a Viewcontroller in Swift
Is [Uiscreen Mainscreen].Bounds.Size Becoming Orientation-Dependent in Ios8
How to Detect First Time App Launch on an Iphone
Uiview Infinite 360 Degree Rotation Animation
How to Open Phone Settings When a Button Is Clicked
How to Create a Button Programmatically
Iphone/Ipad: How to Make Uitextfield Readonly (But Not Disabled)
What Are Unwind Segues For and How to Use Them
Launch an App from Within Another (Iphone)
Ios 9 Not Opening Instagram App With Url Scheme
How to Change the Push and Pop Animations in a Navigation Based App
Number of Days Between Two Nsdates