当试图获得我的useparams()的价值时,我会变得不确定。

发布于 2025-02-11 10:06:06 字数 2289 浏览 1 评论 0原文

使用UseParams()挂钩时,我正在遇到一些问题。

我有4个组件:

app.js,midlocal.js,blogposts.js和blogsingle.js,

我在app.js中设置了路由,

<Route path='/posts/:id' element={<PostSingle />} />

在midlocal.jsx sill中进行了API呼叫

  const MidLocal = () => {
  const [post, setPost] = useState([]);
  


  async function fetchPosts() {
    const response = await fetch("https://khg.com.ng/wp-json/wp/v2/posts")
    const data = await response.json();

    setPost(data);
      

  };

  useEffect(() => {
    fetchPosts();
  }, []);

然后我在同一中局部组件中 ,我正在使用array.map()并将数据传递给组件,

<div className="mid__blog__posts">
          {post.map((item) => (
            <BlogPosts
              key={item.id}
              postId = {item.id}
              title={item.title.rendered}
              date={item.date}
              img={item.img}
            />
          ))}
        </div>

这是Blogpost组件。只是一个简单的功能组件,可以按照我喜欢的方式来安排我的数据,并使用React路由器的链接功能使用其属性来指向单个博客页面/组件

const BlogPosts = ({postId, date, title, img}) => {
  
  return (
    <div className='post__container'>
                    <div className='post__texts'>
                        <small className='md__post__date'>{date}</small>
                        <Link to={`/posts/${postId}`}><h3 className='md__post__title'>{title}<span className='post__url__icon'><BsFillArrowRightCircleFill /></span></h3></Link>
                    </div>
      
                    <img src={img} alt={title} className='post__img'/>
                </div>
  )
}

export default BlogPosts

,然后在博客单个组件中,

const BlogSingle = () => {
  const [content, setContent] = useState([]);

  const {postId} = useParams();

  

  console.log(postId)

  async function fetchPostContent() {
    const response = await fetch(`https://khg.com.ng/wp-json/wp/v2/posts/${postId}`)
    const data = await response.json();

    setContent(data);
      
  };

  useEffect(() => {
    fetchPostContent();
  }, []);

console.log(PostID)给出。我不确定

,试图访问邮政的价值也使我不确定


<h1>{postId}</h1>

I am experiencing some issues reading the value of my id when using useParams() hook.

I have 4 components:

App.js, Midlocal.js, Blogposts.js and BlogSingle.js

I have the routing set up in App.js

<Route path='/posts/:id' element={<PostSingle />} />

Then I made my Api call in MidLocal.jsx

  const MidLocal = () => {
  const [post, setPost] = useState([]);
  


  async function fetchPosts() {
    const response = await fetch("https://khg.com.ng/wp-json/wp/v2/posts")
    const data = await response.json();

    setPost(data);
      

  };

  useEffect(() => {
    fetchPosts();
  }, []);

Sill in the same MidLocal component, I am using array.map() and passing the data as props to component

<div className="mid__blog__posts">
          {post.map((item) => (
            <BlogPosts
              key={item.id}
              postId = {item.id}
              title={item.title.rendered}
              date={item.date}
              img={item.img}
            />
          ))}
        </div>

This is the BlogPost component. Just a simple functional component where my data could be arranged the way I like it with the react router's Link feature using its TO attribute to point to the single blog page/component

const BlogPosts = ({postId, date, title, img}) => {
  
  return (
    <div className='post__container'>
                    <div className='post__texts'>
                        <small className='md__post__date'>{date}</small>
                        <Link to={`/posts/${postId}`}><h3 className='md__post__title'>{title}<span className='post__url__icon'><BsFillArrowRightCircleFill /></span></h3></Link>
                    </div>
      
                    <img src={img} alt={title} className='post__img'/>
                </div>
  )
}

export default BlogPosts

Then, in the Blog Single component,

const BlogSingle = () => {
  const [content, setContent] = useState([]);

  const {postId} = useParams();

  

  console.log(postId)

  async function fetchPostContent() {
    const response = await fetch(`https://khg.com.ng/wp-json/wp/v2/posts/${postId}`)
    const data = await response.json();

    setContent(data);
      
  };

  useEffect(() => {
    fetchPostContent();
  }, []);

console.log(postId) gives me undefined

and trying to access the value of postId gives me undefined also


<h1>{postId}</h1>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅气称霸 2025-02-18 10:06:06

您需要相应地将ID命名。您将其命名为ID,然后尝试以postid访问它。要么重命名包含/posts/:postid的路径路径,要么在您的blogpost中使用它作为idconst {id} = useparams();

You need to name the id in the route path accordingly. You named it id and you are trying to access it as postId. Either rename the path in route to contain /posts/:postId or use it as id in your BlogPost: const { id } = useParams();

云归处 2025-02-18 10:06:06

只需破坏ID并将其重命名为PESTID,请参见以下代码以获取快速参考!

const BlogSingle = () => {
  const [content, setContent] = useState([]);

  const {id: postId} = useParams();

  

  console.log(postId)

  async function fetchPostContent() {
    const response = await fetch(`https://khg.com.ng/wp-json/wp/v2/posts/${postId}`)
    const data = await response.json();

    setContent(data);
      
  };

  useEffect(() => {
    fetchPostContent();
  }, []);

Just destructure id and rename as postId, see the following code for quick reference!

const BlogSingle = () => {
  const [content, setContent] = useState([]);

  const {id: postId} = useParams();

  

  console.log(postId)

  async function fetchPostContent() {
    const response = await fetch(`https://khg.com.ng/wp-json/wp/v2/posts/${postId}`)
    const data = await response.json();

    setContent(data);
      
  };

  useEffect(() => {
    fetchPostContent();
  }, []);

半夏半凉 2025-02-18 10:06:06
let { id: postId } = useParams();
let { id: postId } = useParams();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文