簡體   English   中英

react-redux - 在 hooks 文檔中,為什么在調用 createSelector() 時將選擇器的參數放入單獨的參數選擇器中?

[英]react-redux - in the hooks documentation, why is a parameter of the selector put in a separate parametric selector when calling createSelector()?

react-redux 文檔useSelector鈎子示例中,有一個代碼片段:

const selectNumOfTodosWithIsDoneValue = createSelector(
  state => state.todos,
  (_, isDone) => isDone,
  (todos, isDone) => todos.filter(todo => todo.isDone === isDone).length
)

正如我們所見, isDoneselectNumOfTodosWithIsDoneValue一個參數。 但是為什么它放在一個單獨的參數選擇器中,即(_, isDone) => isDone 我可以寫如下嗎?

const selectNumOfTodosWithIsDoneValue = createSelector(
  (state, isDone) => state.todos.filter(todo => todo.isDone === isDone),
  filteredTodos => filteredTodos.length
)

這兩種方法有什么區別?

第二個示例將始終導致完全重新計算,因為filter()始終返回一個新的數組引用。

雖然我對當前的(第一個)示例不是特別滿意,但它確實記住了,因為它只會在state.todosisDone更改時重新計算長度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM