问题描述
逻辑:
- 利用hooks的
useEffect
发送接口请求,数据回传后写入useState里 - 在组件内容里渲染数据
遍历时ts
检测content不存在,请问如何解决?
export const Joke = (props: any) => {
const [jokeList, setJokeList] = useState([]);
useEffect(() => {
const asyncFetch = async () => {
await fetchJokeList().then(result => {
setJokeList(result.data);
});
}
asyncFetch();
}, []);
return (
<div>{jokeList.map(item => ( // const jokeList: never[]
<p>
{item.content} // 此行报错,类型“never”上不存在属性“content”
</p>
))}</div>
)
}
item新增接口定义时,报错
item['content']