I'm getting an undefined error on value variable which is a state but when i console the variable in which data is Store it Give Correct Data From Data Base but when a put that varible to set state it show undefined Value.
import React, { useState } from "react";
import { useEffect } from "react";
const TodoList = () => {
const [Printed, SetDbData] = useState();
const getData = async () => {
try {
const getDbContent = await fetch("/getData", {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
credentials: "include",
});
const getDbData = await getDbContent.json();
const [object] = getDbData;
SetDbData(object);
console.log(object);
//************** when i console object is Show data **************
{
"_id": "62d145c8ea501901f8ad650d",
"todoText": "hello",
"__v": 0
}
console.log(Printed);
//************* but when i console state varriable (Printed )is Show undefined Data***********
(undefined)
} catch (e) {
console.log(e);
}
};
useEffect(() => {
getData();
}, []);
return (
<>
<div className="container">
<div className="card">
<br />
<h1>Todo List</h1>
<span></span>
<br />
<form className="inner_card" method="POST" >
<input
type="text"
name="todoText"
placeholder="Add Items"
autoComplete="off"
/>
<button type="submit" >
+
</button>
<span className="warning_tag" id="span_id">
WARNING : Please Add Something
</span>
</form>
</div>
</div>
</>
);
};
export default TodoList;
plaese Can Anyone tell my where i'm doing mistake i'm seriously stuck in this problem please help me