gpt4 book ai didi

reactjs - 使用 react-redux 翻译函数时呈现的钩子(Hook)比预期的少

转载 作者:行者123 更新时间:2023-12-02 00:15:28 25 4
gpt4 key购买 nike

我得到一个 Rendered hooks 比预期的要少。这可能是由于意外的提前返回语句引起的。 当我使用以下代码时的消息:

{headRows
// Filter table columns based on user selected input
.filter(item => displayedColumns.includes(item.id))
.map(row => (
<TableCell
key={row.id}
align={row.numeric ? "right" : "left"}
padding={row.disablePadding ? "none" : "default"}
sortDirection={orderBy === row.id ? order : false}
>
<TableSortLabel
active={orderBy === row.id}
direction={order}
onClick={createSortHandler(row.id)}
>
{useTranslation(row.label)}
</TableSortLabel>
</TableCell>
))}

我的翻译函数是这样的:

import { useSelector } from "react-redux";

export const useTranslations = () =>
useSelector(state => state.translations.data, []);

如果我向其中传递一个字符串,翻译函数就会按预期工作。但是,如果我将 {useTranslation(row.label)} 替换为 {row.label},我就不会再收到错误消息。在我看来,我没有在循环、条件或嵌套函数中调用 Hooks,还是我错了?

最佳答案

您有一个呈现单元格列表的组件。但是这里的每个单元格都由传递给 map 的回调呈现。所以,实际上,您在这里同时拥有循环和嵌套函数。

我建议您将回调提取到一个新组件并进行渲染。在这种情况下,每个单元格都将是一个新组件,您可以自由使用钩子(Hook)。


const MyTableCell = props => {
const {row} = props;
const title = useTranslation(row.label);
return (
<TableCell>
<TableSortLabel>
{title}
</TableSortLabel>
</TableCell>
)

}

// and then

{headRows
// Filter table columns based on user selected input
.filter(item => displayedColumns.includes(item.id))
.map(row => (
<MyTableCell row={row} key={row.id} />
))}

关于reactjs - 使用 react-redux 翻译函数时呈现的钩子(Hook)比预期的少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57055845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com