【发布时间】:2020-11-16 15:42:05
【问题描述】:
const [todos, setTodos] = useState([])
useEffect(() => {
todolist()
},[])
const todolist = () => {
if (typeof window !== undefined) {
if (localStorage.getItem("jwt")) {
let temp = JSON.parse(localStorage.getItem("jwt"));
getToDo(temp.user._id, temp.token).then(response => {
setTodos(response)
}).catch((err) => {
console.log(err)
})
}
}
}
const getToData = () => {
console.log('todo', todos.todos)
}
2.当我应用 .map 函数时,我得到一个错误TypeError: Cannot read property 'map' of undefined
const getToData = () => {
console.log('todo', todos.todos)
let userTodos = todos.todos
//here is where i get error
return userTodos.map((current, index) => {
return <Todo todo={current} key={index} />
})
}
你能帮我解决这两个问题吗
【问题讨论】:
标签: javascript reactjs react-hooks array.prototype.map