0

Based on the React-Native-Calendars documentation, here's the test codes with hard-coded markedDates that show the dot(s) on the calendar:

const FirstDot = { key: 'First', color: 'blue' };
const SecondDot = { key: 'Second', color: 'blue' };

<Calendar
   current={new Date()}
   markingType={'multi-dot'}
   markedDates={{
      '2019-04-15': { dots: [FirstDot, SecondDot] },
      '2019-04-14': { dots: [FirstDot] },
   }}
/>

Basically I want to show one or two dots depending on the data. Let's say I am retrieving the data from SQLite, how can I populate the markedDates dynamically in React Native?

this.state = { markedDates: {} };  //how to declare the state object?
//other codes.....

db.transaction((tx) => { 
    let objMarkedDates = this.state.markedDates;
    tx.executeSql('SELECT myDates, dataDots FROM myTable', 
    [], (tx, results) => {
        const len = results.rows.length;
        if (len > 0) {
            for (let i = 0; i < len; ++i) {
                if (results.rows.item(i).dataDots === 2) { //show 2 dots
                    //How to populate the date and the dots to the object???
                }
            }
        }
    }); 

    this.setState({ markedDates: objMarkedDates });
});

<Calendar
   current={new Date()}
   markingType={'multi-dot'}
   markedDates={this.state.markedDates}
/>
Mochi08
  • 167
  • 3
  • 15

2 Answers2

0

Assuming if your data is in array form, here is what i wrote for json data array

            var element = {};

            for(var i =0;i<this.state.array.length;i++){
                if((Object.keys(element)).indexOf(this.state.array[i].date) > -1){
                  for(var j = 0;j<Object.keys(element).length;j++){
                    if(this.state.array[i].start_date === Object.keys(element)[j]){
                      var a ={key:this.state.array[i].id,color:'red'}
                      element[this.state.array[i].date].dots.push(a);
                    }
                  }
                }else{
                  var a = {dots:[{key:this.state.array[i].id,color:'red'}]}
                  element[this.state.array[i].date+''] = a;
                }
            }
0

Based on the user11630092 answer:

const MARKED_DATE_CONTAINER: ViewStyle = {
    backgroundColor: AppColors.primary,
    borderRadius: 0
}

const MARKED_DATE_TEXT: TextStyle = {
    color: AppColors.white
}

enter image description here

const customStyles = {
    container: {...MARKED_DATE_CONTAINER},
    text: {...MARKED_DATE_TEXT}
}
var element = {};

for(var i =0;i<array.length;i++){
    var a = {customStyles: {...customStyles}}
    element[`${formatMyDate(array[i].startDate)}`] = a;
}

RESULT

Chinedu Ofor
  • 707
  • 9
  • 11