嵌套组件不会接收由父组件传递的类型。

huangapple go评论68阅读模式
英文:

Nested component wont receive types passed down by parent

问题

以下是您要的代码部分的翻译:

// 我试图组装一个表格,它将通过后端解决的操作进行更新,因此我需要能够传递端点和响应模型,当我尝试嵌套并传递响应类型时,它在子组件中不知何故丢失了。

// 简要示意图:TableParent.ResT -> TableChild ProviderForUpdates DataHandler.ResT // 需要这个来知道如何处理数据 ProviderForUpdates / TableChild

// 想要的情景:父组件将传递响应类型 - DataHandlerChild 将继承它并进行查询以更新表格 - 子组件将相应更新

import { useState } from "react";
import DataTable from "@components/dataTable";

type CarTypes = { id: number; name: string; description: string; };

interface ResponseType {
  id: number;
  carTypes: CarTypes[];
}

const CarTypeComponent = () => {
  const [carTypes, setCarTypes] = useState<ResponseType[]>([]);
  
  return (
    <>
      <DataTable<ResponseType>
        endpoint="car_types"
        setIdsSelected={(ids: number[]) => setIdsSelected(ids)}
        idsSelected={idsSelected}
        onRowClick={handleRowClick}
      />
    </>
  );
};

const DataTable = <ResponseType>({
  setIdsSelected,
  idsSelected,
  onRowClick,
  endpoint,
  onSort = (column: string, dir: "asc" | "desc" | "") => {},
}: Props) => {
  return (
    <>
      <TableContentProvider>
        {/* 由于我不知道的原因,类型在这里不会出现 */}
        <TableDataHandler<ResponseType>
          setIdsSelected={setIdsSelected}
          idsSelected={idsSelected}
          onSort={onSort}
          onRowClick={(id: number) => {
            onRowClick(id);
          }}
          endpoint={endpoint}
        />
      </TableContentProvider>
    </>
  );
};

const TableDataHandler = <ResponseType>({
  setIdsSelected,
  idsSelected,
  onSort,
  onRowClick,
  endpoint,
}: TableDataHandlerProps<ResponseType>) => {
  const { currentSortedColumn, queryString } = useTableContext();
  const { response, error, loading } = useAxios<ResponseType>({
    url: `${endpoint}?${queryString}`,
    method: Methods.GET,
  });
  
  return (
    <>
      {response && <TableCore<typeof tableData.data />}
    </>
  );
};

请注意,这个代码可能需要根据您的项目和库的实际情况进行调整,以确保一切正常运作。

英文:

I am trying to assemble a table that will be updated through actions resolved by backend, so for this reason I need to be able to pass the endpoint and the response model , when I try to nest and pass the response type it somehow gets lost in children components.

brief schematic TableParent.ResT -> TableChild ProviderForUpdates DataHandler.ResT\ // needs this to know under how to deal with the data ProviderForUpdates/ TableChild

Wanted scenario ParentComponent -will pass down the Type of Response - DataHandlerChild will inherit it and make the queries to update table - Children will update accordingly

Thanks for your help in advance

import { useState } from &quot;react&quot;; import DataTable from 
&quot;@components/dataTable&quot;; type CarTypes = { id: number; name: string; description: string; }; interface ResponseType { id: number; carTypes: CarTypes[]; }
const CarTypeComponent = () = &gt;{
const[carTypes, setCarTypes] = useState &lt; ResponseType[] &gt; ([]);
return ( &lt; &gt;&lt;DataTable &lt; ResponseType &gt; endpoint = &quot;car_types&quot;setIdsSelected = { (ids: number[]) = &gt;setIdsSelected(ids)
}
idsSelected = {
idsSelected
}
onRowClick = {
handleRowClick
}
/&gt;
&lt;/ &gt; );
};
const DataTable = &lt;ResponseType,
&gt;({
setIdsSelected,
idsSelected,
onRowClick,
endpoint,
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function, react/prop-types
onSort = (column: string, dir: &quot;asc&quot; | &quot;desc&quot; | &quot;&quot;) = &gt;{},
}: Props) = &gt;{
return (&lt; &gt;{
} &lt; TableContentProvider &gt; {
/* for reasons beyond my knowledge type doesnt arrives here */
} &lt; TableDataHandler &lt; ResponseType &gt; setIdsSelected = {
setIdsSelected
}
idsSelected = {
idsSelected
}
onSort = {
onSort
}
onRowClick = { (id: number) = &gt;{
onRowClick(id);
}
}
endpoint = {
endpoint
}
/&gt;
&lt;/TableContentProvider &gt; &lt;/&gt;
);
};
const TableDataHandler = &lt;ResponseType,&gt;({
setIdsSelected,
idsSelected,
onSort,
onRowClick,
endpoint,
}: TableDataHandlerProps&lt;ResponseType&gt;) =&gt; {
const { currentSortedColumn, queryString } = useTableContext();
const {
response,
error,
loading
} = useAxios &lt; ResponseType &gt; ({
url: `$ {
endpoint
}
s ? $ {
queryString
}`,
method: Methods.GET,
});
return ( &lt; &gt;{
response &amp;&amp; &lt;TableCore &lt; typeof tableData.data &gt; /&gt;
}
&lt;/ &gt; );
};

答案1

得分: 1

你不能这样传递属性,尝试使用更简单的方法。

function CarTypeComponent({ ids }: { ids: number[] }) {
  const [carTypes, setCarTypes] = useState([]);
  const [idsSelected, setIdsSelected] = useState<number[]>([]);

  const handleIdSelected = () => {
    setIdsSelected(ids);
  };

  return (
    <DataTable setIdsSelected={handleIdSelected} endpoint="/api/carTypes" />
  );
}
英文:

you can not pass props like that try something simpler

function CarTypeComponent({ ids }: { ids: number[] }) {
const [carTypes, setCarTypes] = useState([]);
const [idsSelected, setIdsSelected] = useState&lt;number[]&gt;([]);
const handleIdSelected = () =&gt; {
setIdsSelected(ids);
};
return (
&lt;DataTable setIdsSelected={handleIdSelected} endpoint=&quot;/api/carTypes&quot; /&gt;
);
}

huangapple
  • 本文由 发表于 2023年3月7日 22:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定