0

While trying to make an app that would ask for 3 dates, i've stumbled on a problem :

I have this:

onDateChange = (state) => (event, value) => {
    this.setState({
        [state]: name
    });
}

that is called by:

<DatePicker
    dateMes={this.state.dateMes}
    mode="date"
    placeholder="select date"
    format="DD-MM-YYYY"
    minDate="01-01-1950"
    maxDate="01-01-2050"
    androidMode="spinner"
    showIcon={false}
    onDateChange={this.onDateChange('dateMes')}
/>

When loaded, my 3 datepickers are juste filled with the placeholder. When i open them and select a date, nothing will change (as if i never selected a date)

I believe it's not finding how to make the onDateChange work, but I don't know how to make it work

Thanks for the help !

EDIT: Thanks to Chris G, i got a solution. My function now looks like

onDateChange = (state) => (event, name) => {
    this.setState({
        [state]: name
    });
}

The error disappeared, but the date are not changed with the datepicker.

2
  • In your parameters you have value but you're using name in your setState call. Commented Jul 12, 2017 at 8:24
  • Well that is indeed a rather dumb error. Although the error doesn't pops anymore, the dates still don't change :/. Commented Jul 12, 2017 at 8:44

3 Answers 3

1

Let me give this some perspective , OnDateChange prop receives a function which act as a callback you are assigning it fine , but when the callback is invoked reference of 'this' has changed and it couldnt find the variables which are bounded by the class

Now in react-native example they have used Arrow function , which lexically bound the scope of 'this' which explains why you could find the class variables because 'this' still refers to component. Reference : Arrow Functions

Sign up to request clarification or add additional context in comments.

2 Comments

Well thanks for the explanation, it is indeed helping me understand how this works. I'm still new to react-native (and js), so I mostly have problems with the syntax. I will read the arrow function page when i have the time ! :)
sure , you can also use bind function . go through that also , till then to every callback use arrow functions. Cheers!
0

I actually only had to go on the DatePicker official github. They have an example, where they do something like :

<DatePicker
    date={this.state.dateMes}  //it was dateMes=..., wich just meant nothing
    mode="date"
    placeholder="select date"
    format="DD-MM-YYYY"
    minDate="01-01-1950"
    maxDate="01-01-2050"
    androidMode="spinner"
    showIcon={false}
    onDateChange={(dateMes) => { this.setState({ dateMes: dateMes }); }}
/>

Without any function or whatever

And, well, it works. That's all :).

1 Comment

Just FYI, (dateMes) => { this.setState({ dateMes: dateMes }); } is an anonymous function.
0

When you have done setState you must clearly define it in the constructor.Try this

<DatePicker
date={this.state.dateMes}  //it was dateMes=..., wich just meant nothing
mode="date"
placeholder="select date"
format="DD-MM-YYYY"
minDate="01-01-1950"
maxDate="01-01-2050"
androidMode="spinner"
showIcon={false}
 onDateChange={(date) => {this.setState({date: date})}}/>

constructor(props) {
    super(props);
  this.state = {
                date:''

  };
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.