christopherdro / react-native-calendar

[DEPRECATED] Calendar Component for React Native
MIT License
536 stars 244 forks source link

Events are not shown when adding events to array dynamically #137

Open dongawli opened 7 years ago

dongawli commented 7 years ago

Hi,

I am adding events dynamically to the calendar using string .

Here is the code for same ( this code is not working) var eventstr = "{date: '"+ this.state.SelectedDate + "', hasEventCircle: {backgroundColor: '" + event1color +"'}}"; var event1 = [{eventstr}]; let updatedArray = this.state.eventItems.concat(event1); this.setState({eventItems:updatedArray});

Whereas, if I declared event array like this then its showing properly.

var event1 = [{date: '2017-05-19', hasEventCircle: {backgroundColor: '#ccedcc'}}]; let updatedArray = this.state.eventItems.concat(event1); this.setState({eventItems:updatedArray});

Can you please help me what is wrong with 1st approach ? How to construct the event string and add to the events array?

Thank you.

noitcudni commented 7 years ago

@dongawli A string representation of a data structure is different from an actual data structure. If you really want and need your first approach to work, you might try calling eval on the expression.

Then again, why would you want to do that? Maybe you have a good reason. Instead, you might want to construct the data structure (an array of associative arrays) dynamically.