admin管理员组文章数量:1344597
I'm trying to use useMemo
but a I got a message from lint:
React Hook useMemo has a plex expression in the dependency array. Extract it to a separate variable so it can be statically checked react-hooks/exhaustive-deps.
What could it be?
const [selectedDate, setSelectedDate] = useState("Escolha uma data para pagamento");
const itens = useMemo(() => {
if (!contrato.propostas || contrato.propostas.length == 0) return [];
let dates = contrato.propostas.map(p => p.vencimento)
dates = Array.from(new Set(dates)).map((item) => ({
id: item,
option: item
}))
if (dates.length === 1 && selectedDate !== contrato.filterByDate) setSelectedDate(dates[0].option)
if (contrato.filterByDate && dates.find(d => d.id === contrato.filterByDate) !== -1) setSelectedDate(contrato.filterByDate);
return dates;
}, [JSON.stringify(contrato.propostas), contrato.filterByDate, selectedDate])
contrato
and contrato.propostas
are objects.
contrato.filterByDate
is a string.
I tried to find an open question but I couldn't find one.
I'm trying to use useMemo
but a I got a message from lint:
React Hook useMemo has a plex expression in the dependency array. Extract it to a separate variable so it can be statically checked react-hooks/exhaustive-deps.
What could it be?
const [selectedDate, setSelectedDate] = useState("Escolha uma data para pagamento");
const itens = useMemo(() => {
if (!contrato.propostas || contrato.propostas.length == 0) return [];
let dates = contrato.propostas.map(p => p.vencimento)
dates = Array.from(new Set(dates)).map((item) => ({
id: item,
option: item
}))
if (dates.length === 1 && selectedDate !== contrato.filterByDate) setSelectedDate(dates[0].option)
if (contrato.filterByDate && dates.find(d => d.id === contrato.filterByDate) !== -1) setSelectedDate(contrato.filterByDate);
return dates;
}, [JSON.stringify(contrato.propostas), contrato.filterByDate, selectedDate])
contrato
and contrato.propostas
are objects.
contrato.filterByDate
is a string.
I tried to find an open question but I couldn't find one.
1 Answer
Reset to default 7It's telling you to do this:
const [selectedDate, setSelectedDate] = useState("Escolha uma data para pagamento");
const temp = JSON.stringify(contrato.propostas);
const itens = useMemo(() => {
// ...
}, [temp, contrato.filterByDate, selectedDate])
But i don't see why you're stringifying contrato.propostas
. You never use the stringified version, so it's not actually a dependency. And as long as you're keeping your data immutable (as you should in react), then you just need to put contrato.propostas directly into the dependency array:
const [selectedDate, setSelectedDate] = useState("Escolha uma data para pagamento");
const itens = useMemo(() => {
// ...
}, [contrato.propostas, contrato.filterByDate, selectedDate])
本文标签:
版权声明:本文标题:javascript - React - useMemo has a complex expression - Extract it to a separate variable so it can be statically checked - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743748190a2532151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论