最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - React - useMemo has a complex expression - Extract it to a separate variable so it can be statically checked - Stac

matteradmin4PV0评论

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.

Share Improve this question edited Mar 24, 2020 at 18:03 Henfs 5,41112 gold badges32 silver badges47 bronze badges asked Mar 24, 2020 at 17:59 Joao GiovanniJoao Giovanni 1111 gold badge1 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

It'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])

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far