I have a simple React Ui which is supposed to get a json file from localhost:8000/todo
and create the Ui in localhost:3000
.
This is the desired output:
So, the two lines which are "Read a book." and "Cycle around town." are not shown. These two lines are supposed to come from localhost:8000/todo
which is a JSON
type information. I feel like I can fetch the data from localhost:8000/todo
, but I don't know how to show them in localhost:3000
, which is my output.
Here is the function that I have for this:
export default function Todos() {
const [todos, setTodos] = useState([])
const fetchTodos = async () => {
const response = await fetch("http://localhost:8000/todo")
const todos = await response.json()
setTodos(todos.data)
}
useEffect(() => {
fetchTodos()
}, [])
return (
<TodosContext.Provider value={{todos, fetchTodos}}>
<AddTodo />
<Stack spacing={5}>
{todos.map((todo) => (
<b>{todo.item}</b>
))}
</Stack>
</TodosContext.Provider>
)
}
{todos.item} is the part that is supposed to print the items, but it doesn't!
Here is the response from localhost:8000/todo
:
Please let me know if you need more information.