反应本机日历markedDates

时间:2020-04-27 14:07:18

标签: reactjs react-native react-native-calendars

在React Native日历中,有markedDate提供了计划中的事件。手动输入的代码如下:
markedDates = {{
'2012-05-16':{selected:true,已标记:true,selectedColor:'blue'},
'2012-05-17':{marked:true},
'2012-05-18':{标记为:true,dotColor:'red',activeOpacity:0},
'2012-05-19':{disabled:true,disableTouchEvent:true}
}}
我该怎么做才能动态地从数组中设置markedDates?

这是render()的那一行:
markedDates = {this.state.selectedDate,this.state.markedDates}

这是在componentDidMount中执行的:
var selectedDate = {}
selectedDate [dateString] = {选中:true,selectedColor:'#c4c4c4',文本:{color:'black'}}}
this.setState({ selectedDate:selectedDate })

Dynamically mark dates in react-native-calendars 这是一个类似问题的链接,但并不能真正回答或我听不懂答案。两者都有可能

1 个答案:

答案 0 :(得分:0)

带有一个看起来像

的数组
['yyyy-mm-dd','yyyy-mm-dd','yyyy-mm-dd']

您可以像这样映射它:

var customMarkedDates = {};
var arrayOfDates =  ['2020-11-06','2020-11-25','2020-08-12'];
arrayOfDates.map((day) => {
          customMarkedDates[day] = {
            customStyles: {
              container: {
                backgroundColor: "red",
              },
              text: {
                color: "white",
                fontWeight: "bold",
              },
            },
          };
        });

this.setState({customMarkedDates: customMarkedDates})

然后,您可以像markedDates={this.state.customMarkedDates}一样在日历中调用它,并在需要时进行更改。