0

react-native-calendars在我的项目中使用。我正在尝试选择多个日期并尝试更改其颜色。当我安慰选定的日期时,我得到了选定的日期。但我无法改变它的颜色。任何帮助都非常感谢。以下是我的代码

日历.js

  selectDate(day) {
        let selectedDate = day.dateString;
        if (this.state.dates[selectedDate]) {
          const newDates = this.state.dates;
          delete newDates[selectedDate]
          this.setState({ dates: newDates });
        } else {
          const newDates = this.state.dates;
          newDates[selectedDate] = [
            { selected: true, startingDay: true, color: '#05A081' },
            { selected: true, endingDay: true, color: '#05A081' },
          ]
          this.setState({ dates: newDates });
          console.log("NEW_ARRAY", this.state.dates);
        }
      }
   ....

    <CalendarList
              minDate={Date.now()}
              pastScrollRange={24}
              futureScrollRange={24}
              markingType={'period'}
              onDayPress={(day) => this.selectDate(day)}
              markedDates={this.state.dates}
            />

以下是控制台结果

NEW_ARRAY 
{2019-09-03: Array(2), 2019-09-04: Array(2), 2019-09-05: Array(2)}
2019-09-03: Array(2)
0: {selected: true, startingDay: true, color: "#05A081"}
1: {selected: true, endingDay: true, color: "#05A081"}
length: 2
2019-09-04: (2) [{…}, {…}]
2019-09-05: (2) [{…}, {…}]
4

2 回答 2

0
  const selectDate=(day)=> {
    let selectedDate = day.dateString;
    let newDates = dates;
    if (dates[selectedDate]) {
      delete newDates[selectedDate]
    } else {
      
      newDates[selectedDate]={selected: true, marked: true, selectedColor: Colors.YELLOW};
      
      
    }
    setdates({...newDates})
  }
于 2020-11-19T06:25:53.713 回答
0

你可以像这样改变颜色。另外,请对文档进行审查。

selectDate(day) {
        let selectedDate = day.dateString;
        if (this.state.dates[selectedDate]) {
          const newDates = this.state.dates;
          delete newDates[selectedDate]
          this.setState({ dates: newDates });
        } else {
          const newDates = this.state.dates;
          newDates[selectedDate] = [
            { selected: true, startingDay: true, color: 'yourColorHere' }, //change color here
            { selected: true, endingDay: true, color: 'yourColorHere' },//change color here
          ]
          this.setState({ dates: newDates });
          console.log("NEW_ARRAY", this.state.dates);
        }
      }
   ....

    <CalendarList
              minDate={Date.now()}
              pastScrollRange={24}
              futureScrollRange={24}
              markingType={'period'}
              onDayPress={(day) => this.selectDate(day)}
              markedDates={this.state.dates}
            />
于 2019-08-30T12:42:08.087 回答