I am working with react typescript and can not change state in typescript. Within editing function I have setId and setEdit, so after executing editing function it does not change the state.
Here code below:
import React, { useState, useRef, useEffect, useCallback } from "react";
import "../App.css";
const HomePage: React.FC = () => {
const [todo, setTodo]: any = useState([]);
const [id, setId]: any = useState(null);
const [edit, setEdit]: any = useState(false);
const ref = useRef<HTMLInputElement>(null);
const editing = (nameId: any, nameTitle: any) => {
setId(nameId);
setEdit(true);
console.log(id);
console.log(edit);
ref.current!.value = nameTitle;
};
return (
<div className="container">
<div id="task-container">
<div id="list-wrapper">
{todo.map((todo: any, index: number) => {
return (
<div key={index} className="task-wrapper flex-wrapper">
<div style={{ flex: 7 }}>
{todo.completed == false ? (
<span>{todo.title}</span>
) : (
<span>{todo.title}</span>
)}
</div>
<div style={{ flex: 1 }}>
<button
onClick={() => { editing(todo.id, todo.title) }}
className="btn btn-sm btn-outline-info">Edit
</button>
</div>
</div>
);
})}
</div>
</div>
</div>
);
};
export default HomePage;
Not sure what i am missing.