我正在尝试使用useMemo,但我收到了来自 lint 的消息:
React Hook useMemo 在依赖项数组中有一个复杂的表达式。将其提取到一个单独的变量中,以便可以静态检查react-hooks/exhaustive-deps。
会是什么呢?
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])
Run Code Online (Sandbox Code Playgroud)
contrato和contrato.propostas是对象。 …