TypeError: Cannot read properties of undefined (reading 'params') in DetailView.js
TypeError: Cannot read properties of undefined (reading 'params') in DetailView.js
我用过 match.params.id 但最近的更新没有帮助。
import { getPost } from '../../service/api';
const DetailView =({ match }) => {
const classes = useStyle();
const url = 'https://images.';
const [post, setPost] = useState({});
useEffect(() => {
const fetchData = async () => {
let data = await getPost(match.params.id);
console.log(data);
setPost(data);
}
fetchData();
}, []);
This is another calling by id code Inside another post-controller.js
export const getPost = async (request, response) => {
try {
const post = await Post.findById(request.params.id);
response.status(200).json(post);
} catch (error) {
response.status(500).json(error)
}
}
尝试使用来自 react-router-dom
的 useParams
钩子:
import { useParams } from 'react-router-dom';
const DetailView =() => {
const { id } = useParams();
useEffect(() => {
const fetchData = async () => {
let data = await getPost(id);
...
}
fetchData();
}, []);
我用过 match.params.id 但最近的更新没有帮助。
import { getPost } from '../../service/api';
const DetailView =({ match }) => {
const classes = useStyle();
const url = 'https://images.';
const [post, setPost] = useState({});
useEffect(() => {
const fetchData = async () => {
let data = await getPost(match.params.id);
console.log(data);
setPost(data);
}
fetchData();
}, []);
This is another calling by id code Inside another post-controller.js
export const getPost = async (request, response) => {
try {
const post = await Post.findById(request.params.id);
response.status(200).json(post);
} catch (error) {
response.status(500).json(error)
}
}
尝试使用来自 react-router-dom
的 useParams
钩子:
import { useParams } from 'react-router-dom';
const DetailView =() => {
const { id } = useParams();
useEffect(() => {
const fetchData = async () => {
let data = await getPost(id);
...
}
fetchData();
}, []);