React-native-calendars: `addMonth` and `substractMonth` not working with ref

Created on 10 Mar 2019  路  4Comments  路  Source: wix/react-native-calendars

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 }) => (




);
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> ) } }

Most helpful comment

Oh, I figured! I need to pass the value to the function 馃う鈥嶁檪锔廠orry lol.
Correct code:

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

All 4 comments

Oh, I figured! I need to pass the value to the function 馃う鈥嶁檪锔廠orry lol.
Correct code:

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

Thanks man!

Many thanks!

Thanks 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

akhilsanker picture akhilsanker  路  4Comments

henrikra picture henrikra  路  3Comments

Yandamuri picture Yandamuri  路  4Comments

dobiedad picture dobiedad  路  4Comments

ramdhanymf picture ramdhanymf  路  4Comments