reactjs - useQuery 有条件地呈现数据
问题描述
我正在尝试使用我的数据库中useQuery
的GET
数据,然后在我的 REACT 组件中呈现数据,但我不断从我的 JSX 中收到一个长度未定义的错误。谁能看看我的使用useQuery
是否不正确?
const SavedBooks = () => {
const [userData, setUserData] = useState({});
const { loading, data } = useQuery(GET_ME);
console.log(loading)
console.log(data?.me)
if (loading) {
return <h2>LOADING...</h2>
} else {
setUserData(data?.me);
}
// create function that accepts the book's mongo _id value as param and deletes the book from the database
const handleDeleteBook = async (bookId) => {
const token = Auth.loggedIn() ? Auth.getToken() : null;
if (!token) {
return false;
}
try {
const response = await deleteBook(bookId, token);
if (!response.ok) {
throw new Error('something went wrong!');
}
const updatedUser = await response.json();
setUserData(updatedUser);
// upon success, remove book's id from localStorage
removeBookId(bookId);
} catch (err) {
console.error(err);
}
};
return (
<>
<Jumbotron fluid className='text-light bg-dark'>
<Container>
<h1>Viewing saved books!</h1>
</Container>
</Jumbotron>
<Container>
<h2>
{userData?.savedBooks.length
? `Viewing ${userData.savedBooks.length} saved ${userData.savedBooks.length === 1 ? 'book' : 'books'}:`
: 'You have no saved books!'}
</h2>
<CardColumns>
{userData?.savedBooks.map((book) => {
return (
<Card key={book.bookId} border='dark'>
{book.image ? <Card.Img src={book.image} alt={`The cover for ${book.title}`} variant='top' /> : null}
<Card.Body>
<Card.Title>{book.title}</Card.Title>
<p className='small'>Authors: {book.authors}</p>
<Card.Text>{book.description}</Card.Text>
<Button className='btn-block btn-danger' onClick={() => handleDeleteBook(book.bookId)}>
Delete this Book!
</Button>
</Card.Body>
</Card>
)
})}
</CardColumns>
</Container>
</>
);
};
数据最终会自己记录在控制台中,但它不会阻止我的 JSX 被渲染,所以我最终 Cannot read property 'length' of undefined
在这个区域周围出现了一个:
<h2>
{userData?.savedBooks.length
? `Viewing ${userData.savedBooks.length} saved ${userData.savedBooks.length === 1 ? 'book' : 'books'}:`
: 'You have no saved books!'}
</h2>
解决方案
发生错误是因为在某些时候savedBooks
=undifiend
所以您需要做的就是检查它是否存在,只需添加另一个?
userData?.savedBooks?.length
<h2>
{loading ? 'LOADING...' :
userData?.savedBooks?.length ?
`Viewing ${userData.savedBooks.length} saved ${userData.savedBooks.length === 1 ? 'book' : 'books'}:`
: 'You have no saved books!'}
</h2>
代码中还有一些东西可能会给你带来问题你不应该直接使用setUserData
内部的功能组件主体,这会导致不必要的重新渲染或进入无限渲染循环。
我建议做这样的事情:
useEffect(() => {
setUserData(data?.me);
}, [data]);
这将使组件仅在data
更改时呈现。
推荐阅读
- c++ - 究竟什么是“位填充”或“填充位”?
- java - 改变相机角度后出现奇怪的线条
- php - 如何在 MYSQL 列中过滤书面消息?
- c# - 运行容器时从路径获取文件
- r - 在 R 中构建超出量的直方图并覆盖广义帕累托 pdf
- servicestack - 负载问题下的 ServiceStack 日志记录请求正文?
- docker - Docker Volumes 中 -volume 和 -volumes-from 的区别
- android - 出现此错误后,Android Studio 如何以快速模式启动?
- c# - 抑制状态错误 CS0266 无法将类型“”隐式转换为“System.Collections.IEnumerable”
- c++ - Embarcadero c ++ 10.1中的__thread没有创建线程特定的变量