【问题标题】:react-native-calendar re-renders to the current month every time I invoke OnDayPress每次我调用 OnDayPress 时,react-native-calendar 都会重新呈现到当前月份
【发布时间】:2019-10-31 23:02:27
【问题描述】:

我正在使用react-native-calendar,当我通过单击日期调用onDayPress 时,日历会跳转到当前月份。

我的意思是假设我点击 11 月 12 日,日历跳转到 10 月,即当前月份。

export default BookingCalendar = () => {
    const [selected, setSelected] = useState()
    return (
        <View>
            <CalendarList 
                theme={{
                    dayTextColor: 'black',  
                    todayTextColor: 'black',  
                    selectedDayTextColor: 'black', 
                    selectedDayBackgroundColor: '#FF5D4E',
                    arrowColor: 'black',                    
                }}
                style={styles.calendarStyle}
                markedDates={{[selected]: {selected: true, disableTouchEvent: true, selectedDotColor: 'orange'}}}
                current={new Date()}
                minDate={new Date()}
                maxDate={addMonths(6)}
                onDayPress={day => setSelected(day.dateString)}
                onDayLongPress={(day) => {console.log('selected day', day)}}
                monthFormat={'MMM yyyy'}
                onMonthChange={(month) => {console.log('month changed', month)}}
                horizontal={true}
                pagingEnabled={true}
            />
        </View>
    )
}

我的想法是,每次调用 onDayPress 时,日历都会重新渲染,并将其重新定位到 current 日期,所以我尝试在 useEffect 内设置今天的日期,所以它只渲染一次当组件安装时,但没有任何区别。

【问题讨论】:

    标签: reactjs react-native react-hooks react-native-calendars


    【解决方案1】:

    current 属性设置为所选日期可以解决我的问题

    【讨论】:

    • 这不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多