React-day-picker: Suggest my idea for Example Page[Selecting multiple days]

Created on 22 Apr 2020  路  2Comments  路  Source: gpbl/react-day-picker

First, react-day-picker was very helpful for me. thank you for you guys effort!!
and I'm not good at english. so I ask for your understanding : )

Is your feature request related to a problem? Please describe.
I followed the Example (http://react-day-picker.js.org/examples/selected-multiple )
and I wanted to get the difference between prevState.selectedDays and this.state.selectedDays in componentDidupdate function
but I couldn't. I thought this issue is about javascript immutability
In example page

handleDayClick(day, { selected }) {
    const { selectedDays } = this.state;
    if (selected) {
      const selectedIndex = selectedDays.findIndex(selectedDay =>
        DateUtils.isSameDay(selectedDay, day)
      );
      selectedDays.splice(selectedIndex, 1);
    } else {
      selectedDays.push(day);
    }
    this.setState({ selectedDays });
  }

I think splice and push method break state's immutability

Describe the solution you'd like
In React, Keeping the state immutability is important
So.. I think it is better to update example page than now
this is example solution

handleDayClick(day, { selected }) {
    const { selectedDays } = this.state;
    const _selectedDays = selectedDays.concat()
    if (selected) {
      const selectedIndex = _selectedDays.findIndex(selectedDay =>
        DateUtils.isSameDay(selectedDay, day)
      );
      _selectedDays.splice(selectedIndex, 1);
    } else {
      _selectedDays.push(day);
    }
    this.setState({ selectedDays: _selectedDays });
  }

how do you guys think about this? : )
Thank you for your time to read this request

Documentation

Most helpful comment

@doonguk, thanks for your suggestion. Definitely a bug in the example :) Would you mind to send a PR with the fix? The file is this one:

https://github.com/gpbl/react-day-picker/blob/v7/docs/src/code-samples/examples/selected-multiple.js

All 2 comments

@doonguk, thanks for your suggestion. Definitely a bug in the example :) Would you mind to send a PR with the fix? The file is this one:

https://github.com/gpbl/react-day-picker/blob/v7/docs/src/code-samples/examples/selected-multiple.js

Going to publish this soon...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vulcanoidlogic picture vulcanoidlogic  路  6Comments

mcapodici picture mcapodici  路  3Comments

magnusohlin picture magnusohlin  路  4Comments

brpontes picture brpontes  路  6Comments

jgatjens picture jgatjens  路  5Comments