React Hooks中使用onClick传递数据的方法
P粉615886660
P粉615886660 2023-08-31 13:43:40
[React讨论组]

点击blogItem时,我如何将数据从blogItem.js组件传递到blogDetails.js组件?然而,blogItem组件是在Home.js组件中导入的。请问我应该在哪里添加点击动作?有人能分享一个例子吗?

CodeSandboX链接 https://codesandbox.io/s/gallant-water-5bmueq?file=/src/blogItem.js:0-1876

// App.js

import { BrowserRouter, Route, Routes, Switch } from "react-router-dom";
import Home from "./src/Home";
import BlogItem from "./src/blogItem";

function App() {
  return (
    <div className="App">
      <BrowserRouter>
        <Routes>
          <Route path="/" element={<Home />}></Route>
          <Route path="/blogItem" element={<BlogItem />}></Route>
        </Routes>
      </BrowserRouter>
    </div>
  );
}
export default App;

//Home.js

import "./styles.css";
import React, { useState, useEffect, useCallback } from "react";
import BlogItem from "./blogItem";
const Home = (props) => {
  const [blogItem, setBlogItem] = useState(props);

  useEffect(() => {
    setBlogItem(props);
  }, [props]);

  return (
    <div id="App">
      <BlogItem />
    </div>
  );
};

export default Home;
  
// BlogItem.js 

import "./styles.css";
import React, { useState, useEffect, useCallback } from "react";

const blogData = [
  {
    id: 1,
    date: "25 Jan 2023",
    photo: "https://picsum.photos/80",
    heading: "Cypress setup blog",
    blogDetails:
      "Best heading added here. The most relevant data added here. Greatest of all time. Wont be a good idea play here always."
  },
  {
    id: 2,
    date: "22 Jan 2022",
    photo: "https://picsum.photos/80",
    heading: "React state details",
    blogDetails:
      "Best heading added here. The most relevant data added here. Greatest of all time. Wont be a good idea play here always."
  }
];

const BlogItem = ({ id, date, photo, heading, blogDetails }) => {
  const [searchResults, setSearchResults] = useState(blogData);

  const [state, setState] = useState({ id, date, photo, heading, blogDetails });

  const Columns = () => (
    <div className="blogItems">
      <div className="row">
        <div className="blogArea">
          {searchResults.map(({ id, date, photo, heading, blogDetails }) => (
            <a key={id}>
              <div className="blogImageSection">
                <img alt="id" src={photo} />
                <div key={id} className="dataArea">
                  <span className="dataDate">{date}</span>
                  <span className="tags">cypress</span>
                  <h3>{heading}</h3>
                  <p>
                    Best heading added here. The most relevant data added here.
                    Greatest of all time. Wont be a good idea play here always.
                  </p>
                  <a href="_blank" className="readmoreLink">
                    Read more →
                  </a>
                </div>
              </div>
            </a>
          ))}
        </div>
      </div>
    </div>
  );
  return <Columns {...state} />;
};

export default BlogItem;

// BlogDetails.js

import React from "react";

const BlogDetails = () => {
  return (
    <div id="App">
      <div className="blogDetailsSection">
        <div className="row">
          <div className="individual-blogs">
            <h2>ddd</h2>
            <p>Blog details</p>
            <pre>
              <code>const data = []; data.push("soccer");</code>
            </pre>
          </div>
        </div>
      </div>
    </div>
  );
};

export default BlogDetails;

P粉615886660
P粉615886660

全部回复(1)
P粉043566314

首先,在index.js文件中创建browserRouter,并从app.js文件中移除它,并为BlogDetails component创建一个Route

<BrowserRouter>
  <Routes>
      <Route path="/" element={<Home />}></Route>
      <Route path="/blogItem" element={<BlogItem />}></Route>
      <Route path="/blogDetails" element={<BlogDetails />}></Route>
   </Routes>
</BrowserRouter>

然后你需要安装'react-router-dom'包。

npm i react-router-dom

BlogItem.js文件中导入useNavigate

import { useNavigate } from "react-router-dom";
const navigate = useNavigate(); // 在函数组件内部使用

现在你可以给你想要点击的element添加onClick,以便重定向到/blogDetails,例如:

<div className="blogImageSection"
onClick={() =>
  navigate('blogDetails', { state: {id, date, photo, heading, blogDetails } }) // 这是如何使用useNavigate传递数据的方法
}> ... </div>

最后,在BlogDetails.js文件中:

import { useLocation } from "react-router-dom";
const location = useLocation(); // 在函数组件内部使用

你可以像这样访问传递的数据:

const myRecievedData = location.state
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号