如何使用挂钩将状态值从一个组件传递到另一个组件

发布于 2025-02-11 06:32:54 字数 2976 浏览 0 评论 0原文

我正在尝试将状态值从一个组件传递到另一个组件。我的要求是开发两个组件,其中组件A处理状态以获取学生详细信息,并且组件B希望在表格格式中从组件B中呈现学生的详细信息。我已经尝试在组件A中导入组件B,然后通过了组件A的状态A。我尝试在组件B中使用MAP函数,并尝试以表格格式显示数据,但我最终以未定义的方式显示请参阅控制台中的数组值。我尝试在组件本身中进行地图,并将其作为道具将其传递给组件B。现在我也得到了不确定的。任何人都可以帮助我做错了什么。提前致谢。我已经写下了代码。

//旧方法 组件A:

  const studentDetails = [
    { studentID: "001", studentName: "StuOne", studentMarks: "80" },
    { studentID: "002", studentName: "StuTwo", studentMarks: "90" },
    { studentID: "003", studentName: "StuThree", studentMarks: "60" },
    { studentID: "004", studentName: "StuFour", studentMarks: "70" },
    { studentID: "005", studentName: "StuFive", studentMarks: "85" },
  ];

  const [studentdata, setStudentData] = useState([studentDetails]);

  return (
    <div>
      <Header />
      <Details stuDetails={studentdata} />
    </div>
  );

**Component B** 
function Details({ stuDetails }) {
  console.log("Studetals", stuDetails);

  return (
    <div>
      <div className="Main">
        <table>
          <tr>
            <th>Student ID</th>
            <th>Student Name</th>
            <th>Student Mark</th>
          </tr>
          {stuDetails.map((val) => {
            console.log("val", val);

            return (
              <tr>
                <td>{val.studentID}</td>
                <td>{val.studentName}</td>
                <td>{val.studentMarks}</td>
              </tr>
            );
          })}
        </table>
      </div>
    </div>

新方法

**Component A :**
function Main() {
  const studentDetails = [
    { studentID: "001", studentName: "StuOne", studentMarks: "80" },
    { studentID: "002", studentName: "StuTwo", studentMarks: "90" },
    { studentID: "003", studentName: "StuThree", studentMarks: "60" },
    { studentID: "004", studentName: "StuFour", studentMarks: "70" },
    { studentID: "005", studentName: "StuFive", studentMarks: "85" },
  ];

  const [studentdata, setStudentData] = useState([studentDetails]);

  return (
    <div>
      <Header />
      {studentdata.map((value) => (
        <Details
          id={value.studentID}
          name={value.studentName}
          mark={value.studentMarks}
        />
      ))}
    </div>
  );
}

**Component B** 

function Details(props) {
  console.log("PROPS", props);
  return (
    <div>
      <div className="Main">
        <table>
          <tr>
            <th>Student ID</th>
            <th>Student Name</th>
            <th>Student Mark</th>
          </tr>
          <tr>{props.id}</tr>
          <tr>{props.name}</tr>
          <tr>{props.mark}</tr>
        </table>
      </div>
    </div>
    
    
 

I am trying to pass the state value from one component to another component. My requirement is to develop two components where component A handles the state for Student Details and component B wants to render the details of student from component B in the table format. Intially I have tried to import component B in component A and I passed the state of the component A. And I tried to use the map function in component B and tried to display the data in table format but I end up with undefined but I can see the array values in console. And I tried to do the map in component A itself and pass the values as props to component B. Now also I am getting the undefined. Any can help me what I am doing wrong. Thanks in advance. I have wrote down the codes.

//Old Approach
Component A :

  const studentDetails = [
    { studentID: "001", studentName: "StuOne", studentMarks: "80" },
    { studentID: "002", studentName: "StuTwo", studentMarks: "90" },
    { studentID: "003", studentName: "StuThree", studentMarks: "60" },
    { studentID: "004", studentName: "StuFour", studentMarks: "70" },
    { studentID: "005", studentName: "StuFive", studentMarks: "85" },
  ];

  const [studentdata, setStudentData] = useState([studentDetails]);

  return (
    <div>
      <Header />
      <Details stuDetails={studentdata} />
    </div>
  );

**Component B** 
function Details({ stuDetails }) {
  console.log("Studetals", stuDetails);

  return (
    <div>
      <div className="Main">
        <table>
          <tr>
            <th>Student ID</th>
            <th>Student Name</th>
            <th>Student Mark</th>
          </tr>
          {stuDetails.map((val) => {
            console.log("val", val);

            return (
              <tr>
                <td>{val.studentID}</td>
                <td>{val.studentName}</td>
                <td>{val.studentMarks}</td>
              </tr>
            );
          })}
        </table>
      </div>
    </div>

New Approach

**Component A :**
function Main() {
  const studentDetails = [
    { studentID: "001", studentName: "StuOne", studentMarks: "80" },
    { studentID: "002", studentName: "StuTwo", studentMarks: "90" },
    { studentID: "003", studentName: "StuThree", studentMarks: "60" },
    { studentID: "004", studentName: "StuFour", studentMarks: "70" },
    { studentID: "005", studentName: "StuFive", studentMarks: "85" },
  ];

  const [studentdata, setStudentData] = useState([studentDetails]);

  return (
    <div>
      <Header />
      {studentdata.map((value) => (
        <Details
          id={value.studentID}
          name={value.studentName}
          mark={value.studentMarks}
        />
      ))}
    </div>
  );
}

**Component B** 

function Details(props) {
  console.log("PROPS", props);
  return (
    <div>
      <div className="Main">
        <table>
          <tr>
            <th>Student ID</th>
            <th>Student Name</th>
            <th>Student Mark</th>
          </tr>
          <tr>{props.id}</tr>
          <tr>{props.name}</tr>
          <tr>{props.mark}</tr>
        </table>
      </div>
    </div>
    
    
 

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文