简体   繁体   中英

How to dynamically populate Calendar marked dates from api - React Native, redux

As title said I need to populate marked dates which im getting from API in Calendar component (i am using "react-native-calendars": "^1.403.0").

enter code here : <Calendar
    markedDates={{
       "2020-09-15": { selected: true, marked: true, selectedColor: "blue" },
       "2020-09-05": { selected: true, marked: true, selectedColor: "blue" },
       }}/>

Solution:

 const CalendarScreen = (props) => {
  const dispatch = useDispatch();
  const access_token = useSelector((state) => state.auth.token);
  const calendar = useSelector((state) => state.calendarClasses.calendar);
  const isCalendarLoading = useSelector(
    (state) => state.calendarClasses.isCalendarLoading
  );

  useEffect(() => {
    dispatch(loadCalendar());
    dispatch(getCalendar(access_token, childId));
  }, [dispatch, getCalendar]);

  let markedDay = {};

  calendar.map((item) => {
    markedDay[item.date] = {
      selected: true,
      marked: true,
      selectedColor: "purple",
    };
  });


  return (
      <Calendar markedDates={ markedDay }
        ...
    />
}

Pay attention in component Calendar - markedDates = { } , markedDates must be in with single {}, because markedDay is object. So you cant set like it is in documentation <Calendar markedDatse={{ markedDay }} /> because this will be like markedDates= {{{ }}}.

You are only missing some square brackets around the date:

markedDates={{
  [day]: { selected: true, marked: true, selectedColor: "blue" },
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM