Skip to content

Commit

Permalink
fix: 修复删除之后添加还在的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
myNameIsDu committed Oct 22, 2022
1 parent 2deadb1 commit fbd1ffd
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/comonent/dish-list.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useState, Fragment, type MouseEvent } from "react";
import { useState, Fragment, type MouseEvent, useRef } from "react";
interface DishesListPropsType {
dishes: string[];
}
export function DishesList({ dishes }: DishesListPropsType) {
const dishesRef = useRef<string[]>([...dishes]);
const [list, setList] = useState(() => {
if (dishes.length) {
// 根据 dishes 长度生成 [0,1,2,3] 的 list
Expand All @@ -18,6 +19,7 @@ export function DishesList({ dishes }: DishesListPropsType) {
const newList = [...list];
index && newList.splice(Number(index), 1);
setList(newList);
index && dishesRef.current.splice(Number(index), 1);
}
};
const handleAdd = () => {
Expand All @@ -33,7 +35,7 @@ export function DishesList({ dishes }: DishesListPropsType) {
option
</label>
<input
defaultValue={dishes[i]}
defaultValue={dishesRef.current[i]}
name={`dishes[${i}]`}
required
max={100}
Expand Down

0 comments on commit fbd1ffd

Please sign in to comment.