简体   繁体   English

如何将数组作为道具传递并在 typescript 反应钩子中的子组件中渲染

[英]How to pass an array as a prop and render in child component in typescript react hooks

I have difficulty passing an array as a prop to a component from the parent and rendering the same in react typescript.我很难将数组作为道具从父组件传递给组件,并在反应 typescript 中呈现相同的组件。

Here is the parent component where the array is been passed.这是传递数组的父组件。

import ReportComponent from '../Components/Reports/ReportComponent';
import { ReportData } from "../Types/ReportData.types";
const Report = () => {
 const [Report, setReport] = useState<ReportData[]>([]);
 ReportService.GetReport()
      .then((response) => {
        console.log(response.data.data);
        setReport(response.data.data);
        toast.success(response.data.message);
      }).catch((e) => {
        console.log(e);
      });

  return <ReportComponent report {...Report}/>;

But I discovered that the array is not getting to the child and I am getting is TypeError: Report.map is not a function但我发现数组没有到达孩子,我得到的是TypeError: Report.map is not a function

import { ReportData } from "../../Types/Report.types";
const ReportComponent = (props:ReportData) => {
console.log("props",props)
const [Report, setReport] = useState<ReportData[]>([]);
setReport(props)
return  <div className="row">
         <div className="table-responsive">
      { Report.map((report)=>(  
      <table className="table table-striped table-sm">
        <thead>
          <tr>
            <th scope="col">Name</th>
            <th scope="col">UID</th>
            <th scope="col">Value</th>
            <th scope="col">Length</th>
          </tr>
        </thead>    
          <tbody className="table table-striped table-sm">
            <tr>
              <td>{report.name}</td>
              <td>{report.UID}</td>
              <td>{report.Value}</td>
              <td>{report.Length}</td>
             </tr>
          </tbody>
      </table>
       ))}
    </div>
        </div>
} 

TL;DR: TL;博士:


const Report = () => {
    const [report, setReport] = useState<ReportData[]>([]);

    useEffect(() => {
        ReportService.GetReport()
            .then((response) => {
                console.log(response.data.data);
                setReport(response.data.data);
                toast.success(response.data.message);
            }).catch((e) => {
                console.log(e);
            });
    }, []);

    return <ReportComponent reports={report} />;
);

interface ReportComponentProps {
    reports: ReportData[];
}

const ReportComponent = ({
    reports,
}: ReportData) => {
    return (
        <div className="row">
            <div className="table-responsive">
                {reports.map((report) => (
                    <table className="table table-striped table-sm">
                        <thead>
                            <tr>
                                <th scope="col">Name</th>
                                <th scope="col">UID</th>
                                <th scope="col">Value</th>
                                <th scope="col">Length</th>
                            </tr>
                        </thead>
                        <tbody className="table table-striped table-sm">
                            <tr>
                                <td>{report.name}</td>
                                <td>{report.UID}</td>
                                <td>{report.Value}</td>
                                <td>{report.Length}</td>
                            </tr>
                        </tbody>
                    </table>
                ))}
            </div>
        </div>
    );
}
  1. You should not do things as fetching data ( ReportService.GetReport()... ) inside the render of a component.您不应该在组件的渲染中获取数据( ReportService.GetReport()... )。 If you do, every time a component re-renders, that code is executed again, meaning a new fetch will happen.如果这样做,每次组件重新渲染时,该代码都会再次执行,这意味着将发生新的提取。
  2. Passing props is done like <YourComponent propA={propValue} />传递道具就像<YourComponent propA={propValue} />
  3. Your props do not have the type of ReportData ( const ReportComponent = (props:ReportData) => { . props is an object with attributes with the names of the actual props.您的道具没有ReportData的类型( const ReportComponent = (props:ReportData) => {props是一个object ,其属性具有实际道具的名称。
  4. You should not do setState inside the render.您不应该在渲染中执行setState Just like the fetch, every time the component re-renders, that code is executed again.就像 fetch 一样,每次组件重新渲染时,该代码都会再次执行。 Because a setState causes a re-render, that means that the "render code" is executed again, so another setState is executed, that causes another re-render, and so on.因为一个setState会导致重新渲染,这意味着“渲染代码”被再次执行,所以另一个setState被执行,这导致另一个重新渲染,依此类推。
  5. If you recive props by properties , you do not need (and should not) do a setState(props) .如果您通过properties接收props ,则不需要(也不应该)执行setState(props) It is not only redundant, but also causes performance losses.它不仅是多余的,而且还会造成性能损失。

There are a few other issues with your code.您的代码还有一些其他问题。 I encourage you to go through the baiscs of react again.我鼓励你通过再反应的基础来 go

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应,将组件作为子组件传递,然后使用新道具渲染它 - React, pass component as a child and then render it with new prop 使用 React Hooks,当我将 prop 从父组件传递给子组件时,子组件中的 prop 未定义 - Using React Hooks, when I pass down a prop from parent to a child component, the prop in the child component is undefined 如何使用 React 和 typescript 将 prop 传递给组件? - How to pass prop to a component using React and typescript? 如何通过这个 TypeScript React 组件作为可选道具? - How to pass this TypeScript React component an optional prop? 与Typescript反应:无法将函数prop传递给子组件 - React with Typescript:Unable to pass function prop to child component 如何正确地将数组接口/类型传递给 TypeScript React 中的子组件? - How to properly pass array interface/type to a child component in TypeScript React? 如何在反应中使用钩子将数组从子组件传递到父组件 - How to pass an Array from Child component to Parent component using hooks in react 如何正确地将数组从父 React 类组件状态传递给使用钩子编写的子组件? - How to properly pass an array from a parent React class component state to a child component written with hooks? React TypeScript 如何将属性传递给子组件 - React TypeScript how to pass property to child component 如何在带有钩子的子组件上使用 React TypeScript 中的 ref? - How to use ref in React TypeScript on child component with hooks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM