简体   繁体   English

react 和 typescript - 从获取的数据映射 object 数组

[英]react and typescript - mapping an object array from fetched data

recently in my work we are using react with typescript.. And seriously I'm so newbie and I feel really dummy to make any functions.. with it.. But in my free time, I try to learn typescript more.. I'm a little bit confused with hooks in typescript.. Somebody could let me know how to map an object in an object array?最近在我的工作中,我们正在使用与 typescript 的反应。说真的,我是新手,我觉得用它做任何功能真的很笨。但是在我的空闲时间,我尝试学习 typescript 更多。我对 typescript 中的钩子有点困惑。有人可以让我知道如何在 object 数组中使用 map 吗? I've tried many ways and even checked on stackoverflow similar issues, but I couldn't solve mine..我尝试了很多方法,甚至检查了 stackoverflow 类似的问题,但我无法解决我的问题。

Eg usually if I'm using ES6+ I feel really comfortable so in this case, I'd rather show you what I want to get.例如,通常如果我使用 ES6+,我会感觉很舒服,所以在这种情况下,我宁愿向你展示我想要得到的东西。 If I get this easy example I think I will understand typescript more.如果我得到这个简单的例子,我想我会更了解 typescript 。

   const App = () => {
     const [data, setData] = data([]);
     const [query, setQuery] = query("");

     const handleSubmit = (e) => {
        e.preventDefault();
        if (query) {
            const newItem = {id: new Date().getTime().toString(), query};
            setData([...data, newItem]);
        }
     }

     return (
        <section className="section">
            <form onSubmit={handleSubmit}>
                <div className="form-control">
                    <label forHTML="input">Add new</label>
                    <input type="text" id="input"/>
                    <button type="submit">Add</button>
                </div>
            </form>
            <article className="someData">
                {data.map(item => {
                    return <div className="box">
                        <h2>{item.title}</h2>
                    </div>
                })}
            </article>
        </section>
    )
}

I know this example has not much sense, but If I'd know how to write it in typescript I could get know-how typescript works like.我知道这个例子没有多大意义,但如果我知道如何在 typescript 中编写它,我可以了解 typescript 的工作原理。 I've read the documentation and All the time I want to map my object I always type it as Array or object or even as an "any" and nothing does work:( I'm just curious how this function written in ES6 could look like in typescript.. I've read the documentation and All the time I want to map my object I always type it as Array or object or even as an "any" and nothing does work:( I'm just curious how this function written in ES6 could look就像在 typescript..

import React, { useState } from 'react';

interface Data {
  id: string;
  query: string;
}

const App: React.FC<{}> = () => {
  const [data, setData] = useState<Data[]>([]);
  const [query, setQuery] = useState<string>(''); 

  const handleSubmit = (e: React.FormEvent<HTMLFormElement>): void => {
    e.preventDefault();
    if (query) {
      const newItem: Data = { id: new Date().getTime().toString(), query };
      setData([...data, newItem]);
    }
  };

  return (
    <section className="section">
      <form onSubmit={handleSubmit}>
        <div className="form-control">
          <label forHTML="input">Add new</label> {//TS Error, invalid prop forHtml}
          <input type="text" id="input" />
          <button type="submit">Add</button>
        </div>
      </form>
      <article className="someData">
        {data.map((item: Data, i: number) => {
          return (
            <div key={i} className="box">
              <h2>{item.title}</h2>{ // TS Error no title in data}
            </div>
          );
        })}
      </article>
    </section>
  );
};

export default App;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM