【发布时间】:2019-12-18 03:06:15
【问题描述】:
尝试使用 React、NextJS 和 Typescript 做一些事情。我遇到了一个问题,我不断收到“类型'boolean | any []'上不存在x。类型'false'.ts(2339)上不存在属性'x'”错误。我只是不知道在哪里放置界面以显示它使用的是什么类型。谁能帮我吗?这是有问题的代码!
Articles.js
import React, { FunctionComponent } from 'react'; // we need this to make JSX compile
import useFetch from '../hooks/fetch'
type artProps = {
title: string,
author: string,
date: number
}
type article = {
title: string,
author: string,
date: number
}
export const Articles: FunctionComponent<{artProps}> = () => {
const url = 'http://localhost:4000/kb'
const data = useFetch(url)
console.log(data)
return (
<div>
{data.map(articles) => {
return (<div key={article.id}>
{console.log(article)}
<h2 key={article.id}>{article.title} </h2>
<p key={article.id}>{article.body}</p>
</div>
)
})}
{/* {artProps.map(artProp => {
return (
<div>
<h2>{artProp.title}</h2>
<p>{artProp.author}</p>
</div>
)
})
} */}
</div>
)
}
export default Articles
钩子
import React, { useState, useEffect } from 'react';
import axios from 'axios';
const useFetch = (url) => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
console.log('fetch')
axios.get(url)
.then(res => {
if (res.data) {
setData(res.data)
setLoading(false)
}
})
}
, []);
return [data, loading];
};
export default useFetch
【问题讨论】:
标签: reactjs typescript tsx