Probuje ustawic wartość w setEl()
, ale potrzebuje dwoch klikniec aby to zrobic. Syntax z javyscript wystarczal na pojedyncze klikniecie ale w typescript nie dziala
setEl((el) => ({ ...el, old: act }));
co zrobic w takiej sytuacji (moze da sie bez useEffect()
) ?
tu jest kod https://codesandbox.io/s/usereducer-grgc9y?file=/src/Todo.tsx:0-591
interface IMenuItems {
name: string;
id: number;
children?: any[];
}
interface Element {
old: IMenuItems;
act: IMenuItems;
}
type Action =
| { type: "start" }
| { type: "add"; newArr: IMenuItems }
| { type: "root"; data: IMenuItems[] };
function Todo() {
const [el, setEl] = useState({} as Element);
const handle = () => {
setEl((el) => ({ ...el, old: { name: "dddd", id: 2 } }));
console.log(JSON.stringify(el));
};
return (
<div>
<h1>Todos</h1>
<div onClick={() => handle()}>click</div>;
</div>
);
}