瀏覽代碼

reset selected item on datechange

mightyplow 5 年之前
父節點
當前提交
bb805e1817
共有 1 個文件被更改,包括 5 次插入1 次删除
  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,