目标:能够使用prop在日历上选择2个日期react-native-calendars
,onDayPress
并使用prop中的结果markedDates
组成一个天数。
组件.js:
import React, { useState, useEffect } from 'react';
import { Image, View, Animated, StyleSheet, TouchableOpacity, Dimensions } from 'react-native';
import EStyleSheet from 'react-native-extended-stylesheet';
import { Calendar } from 'react-native-calendars';
const { width } = Dimensions.get('window');
const CalendarPicker = (props) => {
const [ markedDates, setMarkedDates ] = useState({});
const markDate = (dateString) => {
setMarkedDates(
(markedDates[dateString] = {
endingDay: true,
color: 'blue'
})
);
};
useEffect(() => {});
return (
<Calendar
style={{
width: width * 0.8
}}
theme={{
arrowColor: '#219F75'
}}
minDate={Date()}
onDayPress={({ dateString }) => markDate(dateString)}
hideArrows={false}
hideExtraDays={true}
hideDayNames={false}
markedDates={markedDates}
markingType={'period'}
/>
);
};
export default CalendarPicker;
问题:什么都没有发生。日期未“标记”,但 useState 变量已正确分配数据。想知道它是否是重新渲染的问题?如何解决此问题以将所选日期显示为“已标记”?