[英]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>
);
}
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.<YourComponent propA={propValue} />
<YourComponent propA={propValue} />
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 ,其属性具有实际道具的名称。setState
inside the render.setState
。 Just like the fetch, every time the component re-renders, that code is executed again.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
被执行,这导致另一个重新渲染,依此类推。props
by properties , you do not need (and should not) do a setState(props)
.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.