如何访问这个taskList数组中的props?

huangapple go评论133阅读模式
英文:

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&lt;Array&lt;object&gt;&gt;()

  const taskCollectionRef = collection(db, &quot;tasks&quot;)

  useEffect(() =&gt; {
    const getTaskList = async () =&gt; {
      // Get data from db
      try {
        const data = await getDocs(taskCollectionRef);
        const filteredData = data.docs.map((doc) =&gt; ({ ...doc.data(), id: doc.id, }));
        setTaskList(filteredData);
        console.log(taskList);
      } catch (err) {
        console.error(err);
      }
    }
    getTaskList();
  }, [])

  return (
    &lt;&gt;
      &lt;TopNavbar title={siteTitle} /&gt;
      &lt;div&gt;
        {taskList?.map((task) =&gt; (
          &lt;div&gt;
            &lt;h1&gt;{task.title}&lt;/h1&gt; /* Property &#39;title&#39; does not exist on type &#39;object&#39;. */ 
          &lt;/div&gt;
        ))}
      &lt;/div&gt;
      &lt;Auth /&gt;
    &lt;/&gt;
  )
}

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&lt;Array&lt;object&gt;&gt;()

  const taskCollectionRef = collection(db, &quot;tasks&quot;)

  useEffect(() =&gt; {
    const getTaskList = async () =&gt; {
      // Get data from db
      try {
        const data = await getDocs(taskCollectionRef);
        const filteredData = data.docs.map((doc) =&gt; ({ ...doc.data(), id: doc.id, } as YOUR_OBJECT_TYPE));
        setTaskList(filteredData);
        console.log(taskList);
      } catch (err) {
        console.error(err);
      }
    }
    getTaskList();
  }, [])

  return (
    &lt;&gt;
      &lt;TopNavbar title={siteTitle} /&gt;
      &lt;div&gt;
        {taskList?.map((task) =&gt; (
          &lt;div&gt;
            &lt;h1&gt;{task.title}&lt;/h1&gt; /* Property &#39;title&#39; does not exist on type &#39;object&#39;. */ 
          &lt;/div&gt;
        ))}
      &lt;/div&gt;
      &lt;Auth /&gt;
    &lt;/&gt;
  )
}

答案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&lt;Array&lt;Task&gt;&gt;()

huangapple
  • 本文由 发表于 2023年6月8日 20:13:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定