I am trying to fetch records from dynamodb using graphql (AWS amplify reacjs setup). I have created a component CTCards and using this in App.js
The problem that we are facing is function fetchTodos is running in infinite loop though in Database there is only two records and I am getting exactly 2 records but this function is running in infinite loop
When I comment the fetching code then the program runs as expected
//== CTCards ( component which is giving issue) ===
function CTCards(props) {
const [todo, setTodos] = useState([]);
useEffect(() => {
fetchTodos();
}, []);
const fetchTodos = async () => {
try {
// == this-block ===
const todoData = await API.graphql(graphqlOperation(listTodos));
const todoList = todoData.data.listTodos.items;
console.log('To Do List', todoList);
setTodos(todoList);
// == /this-block ===
} catch (error) {
console.log('error on fetching to do list', error);
}
};
return (
<div style={{color: "red"}}>
{todo.map((todo, index) => {
<div style={{color: "red"}}>
<div>{todo.name}</div>
<div>{todo.description}</div>
</div>
})}
</div>
)
}
//== App.js code ===
function App() {
return (
<div>
<CTCards/>
</div>
);
}