'How to render state from Redux with map function
Hello I have basic code with useSelector, where data from the useEffect is stored into currentItems.
How to iterate through the state array and render it?
import React, { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { getToDoItems, setItems } from "../redux/globalSlice";
const ToDoList = () => {
const dispatch = useDispatch();
const { currentItems } = useSelector((state) => state.global);
useEffect(() => {
async function getToDoItems(id) {
const response = await fetch(
`https://xxx.mockapi.io/api/list/${id}/todos`
);
const data = await response.json();
dispatch(setItems(data));
}
getToDoItems(2);
}, []);
return (
<div>
<h1>ahoj</h1>
{currentItems.map((todo) => {
<div>{todo.title}</div>;
})}
</div>
);
};
export default ToDoList;
I understand that this is an asynchronous operation, but I don't know how to render it until after loading and storing data to store from the api.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
