Browse Source

reset selected item on datechange

mightyplow 5 years ago
parent
commit
bb805e1817
1 changed files with 5 additions and 1 deletions
  1. 5 1
      src/components/caloriesList/CaloriesList.tsx

+ 5 - 1
src/components/caloriesList/CaloriesList.tsx

@@ -1,4 +1,4 @@
-import React, { useContext, useState } from 'react';
+import React, { useContext, useEffect, useState } from 'react';
 import { AppContext } from '../../layout/AppContext';
 import { haveSameDay } from '../../utils/date';
 import { CaloriesInput } from '../caloriesInput/CaloriesInput';
@@ -11,6 +11,10 @@ function CaloriesList () {
   const {calorieItems = [], setCalorieItems, selectedDate} = useContext(AppContext);
   const [selectedItem, setSelectedItem] = useState<CalorieValue>();
 
+  useEffect(() => {
+    setSelectedItem(undefined);
+  }, [selectedDate]);
+
   function addCalories (addedCalories: { title: string, count: number }) {
     const updatedCalories = [
       ...calorieItems,