React Router 错误 - 未捕获(承诺中)TypeError:无法读取未定义的属性(读取‘路径名’)
2022-04-07
1927
它说未定义
pathname
,但我不知道我的代码出了什么问题。它说当我从后端获取数据到前端时,突然发生了这些错误,并且在它无法执行反应状态更新之前未安装的组件无操作。我已经处理了该错误,但这些错误作为 typeerroror,显示未定义的“pathname”。
section.js 当我取消注释 app.js 时,会出现错误
import React, { useState, useEffect } from 'react'
import './Section.css'
import {Link} from "react-router-dom";
// import api from './api/post';
function Section() {
const [posts, setPosts] = useState([]);
const FetchPosts = async () => {
const response = await fetch('http://localhost:5000/posts');
setPosts(await response.json([]));
}
console.log(posts)
useEffect(() => {
FetchPosts();
}, []);
console.log(posts);
return (
<>
<div className="mainmainsection">
{
posts.map((post) => {
return (
<div key={post._id} className="samplemain">
<div className="samplecontainer">
<div key={post._id} className="r1">
<>
<img className='sectionimg' src={`http://localhost:5000/${post.image}`} alt="myimg" />
<Link to={post.title}></Link>
<h1 className="heading">{post.title}</h1>
</>
</div>
</div>
</div>
)
})
}
</div>
</>
)
}
export default Section
App.js(要呈现的主文件)
import React, { useState, useEffect,useRef } from 'react';
import Footer from './components/Footer';
import Header from './components/Header';
import Navbar from './components/Navbar';
import Missing from './components/MissingPage';
import api from './components/api/post';
import Section from './components/Section';
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Sectioninner from './components/Sectioninner'
import Socialicons from './components/Socialicons';
function App() {
const [posts, setPosts] = useState([]);
const [search, setSearch] = useState('');
const [loading, setLoading] = useState(false);
const [currentPage, setCurrentPage] = useState(1);
const [postsPerPage, setPostsPerPage] = useState(5);
const [searchResults, setSearchResults] = useState([]);
const componentMounted = useRef(true);
useEffect(async() => {
try {
setLoading(true);
const response = await api.get ('http://localhost:5000/posts');
if (componentMounted.current) {
setPosts(response.data);
setLoading(false);
return () => {
componentMounted.current = false;
}
}
} catch (err) {
if (err.response) {
// Not in the 200 response range
console.log(err.response.data);
console.log(err.response.status);
console.log(err.response.headers);
} else {
console.log(`Error: ${err.message}`);
}
}
return () => {
componentMounted.current = false;
}
}, [])
const indexOfLastPost = currentPage * postsPerPage;
const indexOfFirstPost = indexOfLastPost - postsPerPage;
const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost);
return (
<Router>
<Navbar />
<Header search={search} setSearchResults={setSearch} />
<Socialicons />
<Footer />
<Section />
<Routes>
<Route exact path="/" posts={posts} element={<Sectioninner />} />
</Routes>
</Router>
);
}
export default App;
2个回答
我认为这是因为您的某个帖子的标题可能
未定义
。
<Link to={post.title}></Link>
Link
的
to
属性不应为未定义。请尝试检查并修改它。
Shri Hari L
2022-04-07
此错误通知您至少有一个
Link
组件具有未定义的
to
属性。
<Link to={post.title}></Link>
请仔细检查您的数据以确保
post.title
是您想要映射到链接目标的正确属性,或者如果它正确,您将需要有条件地呈现
Link
或过滤映射的
posts
数组。
-
有条件地呈现
Link
{post.title && ( <Link to={post.title}> <h1 className="heading">{post.title}</h1> </Link> )}
-
过滤
posts
数组{posts .filter(post => !!post.title) .map((post) => ( <div key={post._id} className="samplemain"> <div className="samplecontainer"> <div key={post._id} className="r1"> <img className='sectionimg' src={`http://localhost:5000/${post.image}`} alt="myimg" /> <Link to={post.title}> <h1 className="heading">{post.title}</h1> </Link> </div> </div> </div> )) }
Drew Reese
2022-04-07