react-native-calendars icon indicating copy to clipboard operation
react-native-calendars copied to clipboard

`addMonth` and `substractMonth` not working with ref

Open MrAlekhin opened this issue 7 years ago β€’ 8 comments

Please make our job easier by filling this template out to completion. If you're requesting a feature instead of reporting a bug, please feel free to skip the Environment and Reproducible Demo sections.

Description

I am trying to create my own separate buttons for right and left and pass it the bottom of the screen. So I wanna use reference to call addMonth() and substractMonth()

Expected Behavior

I can make a reference of the calendar ref={ref=>this.calendar=ref}. I can call functions this.calendar.addMonth() and this.calendar.substractMonth()

Here is the example with addMonth(): `const ForwardButton = ({ onPress }) => ( Next Month ); class CalendarModal extends PureComponent { constructor(props) { super(props); this.onButtonPressHandler = this.onButtonPressHandler.bind(this); } onButtonPressHandler() { if (this.calendar) { this.calendar.addMonth(); } } render() { const { onDayPress, due_date } = this.props; const date = due_date ? moment(due_date).format("YYYY-MM-DD") : moment() .add(3, "days") .format("YYYY-MM-DD");

let selection = {};
selection[`${date}`] = {
  selected: true,
  // marked: true,
  selectedColor: "#6E64C5"
};
return (
  <View style={styles.root}>
    <View style={styles.main}>
      <View style={styles.header}>
          <ForwardButton onPress={this.onButtonPressHandler} />
      </View>
      <Calendar
        style={styles.calendar}
        minDate={moment(new Date())
          .add(3, "days")
          .toDate()}
        maxDate={moment(new Date())
          .add(10, "days")
          .toDate()}
        onDayPress={date => onDayPress(moment(date.dateString).toDate())}
        markedDates={selection}
        ref={ref => (this.calendar = ref)}
        hideArrows={true}
        theme={{
          "stylesheet.calendar.header": {
            header: {
              height: 0
            }
          }
        }}
      />
      <View style={styles.footer}>
        <TouchableOpacity>
          <View style={styles.doneButton}>
            <Text style={styles.doneButtonText}>Done</Text>
          </View>
        </TouchableOpacity>
      </View>
    </View>
  </View>
);

} }`

Observed Behavior

After pressing on the custom button with reference to calendar it shows me an error:

day.clone is not a function

Calendar.updateMonth
    d6ae6b80-b3d0-44a7-80a4-491b1107edf4:161750:29
Calendar.proxiedMethod
    d6ae6b80-b3d0-44a7-80a4-491b1107edf4:42639:32
CalendarModal.onButtonPressHandler
    d6ae6b80-b3d0-44a7-80a4-491b1107edf4:161389:23
CalendarModal.proxiedMethod
    d6ae6b80-b3d0-44a7-80a4-491b1107edf4:42639:32
Object.touchableHandlePress
    d6ae6b80-b3d0-44a7-80a4-491b1107edf4:46634:40
Object._performSideEffectsForTransition
    d6ae6b80-b3d0-44a7-80a4-491b1107edf4:46155:16
Object._receiveSignal
    d6ae6b80-b3d0-44a7-80a4-491b1107edf4:46084:14
Object.touchableHandleResponderRelease
    d6ae6b80-b3d0-44a7-80a4-491b1107edf4:45963:12
Object.invokeGuardedCallbackImpl
    d6ae6b80-b3d0-44a7-80a4-491b1107edf4:9205:16
invokeGuardedCallback
    d6ae6b80-b3d0-44a7-80a4-491b1107edf4:9296:37

Environment

Also specify:

  1. Phone/emulator/simulator & version:

Reproducible Demo

class CustomCalendar extends Component{ render(){ return ( <View> <TouchableOpacity onPress={()=>this.calendar.addMonth()}><Text>Next Month</Text></TouchableOpacity> <Calendar ref={ref=>this.calendar=ref}/> </View> ) } }

MrAlekhin avatar Mar 09 '19 23:03 MrAlekhin

Oh, I figured! I need to pass the value to the function πŸ€¦β€β™‚οΈSorry lol. Correct code:

  • reference <Calendar ref={ref=>this.calendar=ref}/>
  • usage this.calendar.addMonth(1) or this.calendar.addMonth(-1)

MrAlekhin avatar Mar 09 '19 23:03 MrAlekhin

Thanks man!

BossBele avatar Aug 30 '19 06:08 BossBele

Many thanks!

longvubka98 avatar May 27 '20 08:05 longvubka98

Thanks πŸ˜„

Bangc121 avatar Sep 25 '20 08:09 Bangc121

I'm trying to implement this in the same way but when I console.log calendarRef I see undefined. Also using calendarRef.addMonth, the error calendarRef.addMonth is not a function is shown in the console

mazaherm avatar May 24 '22 13:05 mazaherm

I'm trying to implement this in the same way but when I console.log calendarRef I see undefined. Also using calendarRef.addMonth, the error calendarRef.addMonth is not a function is shown in the console

Same here. Any solution?

Robiullah2244 avatar Jun 01 '22 07:06 Robiullah2244

I'm trying to implement this in the same way but when I console.log calendarRef I see undefined. Also using calendarRef.addMonth, the error calendarRef.addMonth is not a function is shown in the console

Any Solutions ? @MrAlekhin

imshreyansh avatar Jun 08 '22 09:06 imshreyansh

Migration to functional components has broken refs. The current solution is to modify initialDate prop on the calendar component to change the month. Link to discussion here

Changing initialDate triggers a useEffect call which updates the month. Source code for reference:

const Calendar = (props) => {
    const { initialDate, current, theme, disableMonthChange, allowSelectionOutOfRange, minDate, maxDate, onDayPress, onDayLongPress, hideExtraDays, markedDates, firstDay, showSixWeeks, customHeader, headerStyle, displayLoadingIndicator, testID, enableSwipeMonths, accessibilityElementsHidden, importantForAccessibility, onMonthChange, onVisibleMonthsChange, style: propsStyle } = props;
    const [currentMonth, setCurrentMonth] = useState(current || initialDate ? parseDate(current || initialDate) : new XDate());
    const style = useRef(styleConstructor(theme));
    const header = useRef();
    const isMounted = useRef(false);
    useEffect(() => {
        if (initialDate) {
            setCurrentMonth(parseDate(initialDate));
        }
    }, [initialDate]);

yiheinchai avatar Jun 09 '22 08:06 yiheinchai