简体   繁体   English

我不明白如何渲染物体(反应)

[英]I don`t understand how to render objects(react)

I am getting a object from "api" and set it to match but when i try do loop through or render it i get a error. 我从“api”获取一个对象并将其设置为匹配但是当我尝试循环或渲染它时我得到一个错误。

I have tryed Objct.keys maybe my syntax is wrong im not sure im still learning thx for any help. 我试过Objct.keys也许我的语法错了我不知道我还在学习thx任何帮助。

const [match, setMatch] = useState();
const [search, setSearch] = useState('');
const [query, setQuery] = useState(4749875544)

useEffect(() => {
    getData();
}, [query]);
const getData = async () => {
    const response = await 
fetch(`https://api.opendota.com/api/matches/${query}`)
    const result = await response.json();
    setMatch(result);
}   
}
return (
    <div className="App" >
        <form onSubmit={getSearch}
            className="search-form">
            <input className="search-bar"
                type="text"
                value={search}
                onChange={searchInput}
            />
            <Button as="input"
                type="submit"
                value="Submit" />
        </form>
        <li>
        {
      Object.keys(match).map((oneKey,i)=>{
        return (
            <li key={i}>{match[oneKey]}</li>
          )})
        }
        </li>
    </div>
)}

First I would default the state to an Object. 首先,我将状态默认为Object。 It is always good to default your state to the data types you will use. 将状态默认为您将使用的数据类型总是好的。 So at the top useState({}). 所以在顶部useState({})。

React can't render an object. React无法呈现对象。 You have to render each key separately. 您必须单独渲染每个键。 In your map when you return the list item do it with match[oneKey].title or whatever key is actially valid. 在您的地图中,当您返回列表项时,请使用匹配[oneKey] .title或任何密钥有效。

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

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