I am trying to figure out why I have this error when I am mapping on a array of objects for a simple todo project.
I'm very new at Typescript and I don"t know why this happen why my state "list" is well logged in the console as an array.
Can you check what's wrong?
const ToDoListItem = () => {
const [list, setList] = useState();
useEffect(() => {
fetch("http://localhost:1337/lists", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
})
.then((response) => response.json())
.then((data) => setList(data));
}, []);
const findData = async () => {
fetch("http://localhost:1337/lists", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
})
.then((response) => response.json())
.then((data) => setList(data));
};
console.log(list);
return (
<Container>
<Row>
{list.map((e, i) => { //where the issue is coming from
console.log(todo);
return (
<Col xs="12" style={{ display: "flex", justifyContent: "center" }}>
<div className="todo-container">
<InputGroup
style={{
display: "flex",
alignItems: "center",
width: "100%",
justifyContent: "space-evenly",
}}
>
<Input
className="input-text"
value={e.todo}
placeholder="to do"
/>
<Input
type="checkbox"
checked={e.iscompleted}
className="check-box"
/>
question from:
https://stackoverflow.com/questions/65905730/react-typescript-object-is-possibly-undefined-ts2532 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…