英文:
How can I access the props from this taskList array?
问题
I'm trying to access the props from this taskList array that I'm grabbing from firestore,
The taskList is an array of objects so shouldn't I just be able to access them with task.propName?
function App() {
const [taskList, setTaskList] = useState<Array<object>>()
const taskCollectionRef = collection(db, "tasks")
useEffect(() => {
const getTaskList = async () => {
// Get data from db
try {
const data = await getDocs(taskCollectionRef);
const filteredData = data.docs.map((doc) => ({ ...doc.data(), id: doc.id, }));
setTaskList(filteredData);
console.log(taskList);
} catch (err) {
console.error(err);
}
}
getTaskList();
}, [])
return (
<>
<TopNavbar title={siteTitle} />
<div>
{taskList?.map((task) => (
<div>
<h1>{task.title}</h1> /* Property 'title' does not exist on type 'object'. */
</div>
))}
</div>
<Auth />
</>
)
}
export default App
I've tried changing the type in the useState function but I don't know what else it could be other than an array of objects.
英文:
I'm trying to access the props from this taskList array that I'm grabbing from firestore,
The taskList is an array of objects so shouldn't I just be able to access them with task.propName?
function App() {
const [taskList, setTaskList] = useState<Array<object>>()
const taskCollectionRef = collection(db, "tasks")
useEffect(() => {
const getTaskList = async () => {
// Get data from db
try {
const data = await getDocs(taskCollectionRef);
const filteredData = data.docs.map((doc) => ({ ...doc.data(), id: doc.id, }));
setTaskList(filteredData);
console.log(taskList);
} catch (err) {
console.error(err);
}
}
getTaskList();
}, [])
return (
<>
<TopNavbar title={siteTitle} />
<div>
{taskList?.map((task) => (
<div>
<h1>{task.title}</h1> /* Property 'title' does not exist on type 'object'. */
</div>
))}
</div>
<Auth />
</>
)
}
export default App
I've tried changing the type in the useState function but I don't know what else it could be other than an array of objects.
答案1
得分: 2
你可以将对象转换为map:
function App() {
const [taskList, setTaskList] = useState<Array<object>>()
const taskCollectionRef = collection(db, "tasks")
useEffect(() => {
const getTaskList = async () => {
// Get data from db
try {
const data = await getDocs(taskCollectionRef);
const filteredData = data.docs.map((doc) => ({ ...doc.data(), id: doc.id, } as YOUR_OBJECT_TYPE));
setTaskList(filteredData);
console.log(taskList);
} catch (err) {
console.error(err);
}
}
getTaskList();
}, [])
return (
<>
<TopNavbar title={siteTitle} />
<div>
{taskList?.map((task) => (
<div>
<h1>{task.title}</h1> /* Property 'title' does not exist on type 'object'. */
</div>
))}
</div>
<Auth />
</>
)
}
英文:
You can cast the object in map:
function App() {
const [taskList, setTaskList] = useState<Array<object>>()
const taskCollectionRef = collection(db, "tasks")
useEffect(() => {
const getTaskList = async () => {
// Get data from db
try {
const data = await getDocs(taskCollectionRef);
const filteredData = data.docs.map((doc) => ({ ...doc.data(), id: doc.id, } as YOUR_OBJECT_TYPE));
setTaskList(filteredData);
console.log(taskList);
} catch (err) {
console.error(err);
}
}
getTaskList();
}, [])
return (
<>
<TopNavbar title={siteTitle} />
<div>
{taskList?.map((task) => (
<div>
<h1>{task.title}</h1> /* Property 'title' does not exist on type 'object'. */
</div>
))}
</div>
<Auth />
</>
)
}
答案2
得分: 1
If you add the fields coming from the API as a type you can solve it like this:
type Task = {
title: string
complete: boolean
tags: string[]
}
const [taskList, setTaskList] = useState<Array<Task>>([])
英文:
If you add the fields coming from the API as a type you can solve it like this:
type Task = {
title: string
complete: boolean
tags: string[]
}
const [taskList, setTaskList] = useState<Array<Task>>()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论