![](/img/trans.png)
[英]react-redux useSelector hook disallows passing props directly to the selector
[英]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
)
正如我們所見, isDone
是selectNumOfTodosWithIsDoneValue
一個參數。 但是為什么它放在一個單獨的參數選擇器中,即(_, isDone) => isDone
? 我可以寫如下嗎?
const selectNumOfTodosWithIsDoneValue = createSelector(
(state, isDone) => state.todos.filter(todo => todo.isDone === isDone),
filteredTodos => filteredTodos.length
)
這兩種方法有什么區別?
第二個示例將始終導致完全重新計算,因為filter()
始終返回一個新的數組引用。
雖然我對當前的(第一個)示例不是特別滿意,但它確實記住了,因為它只會在state.todos
或isDone
更改時重新計算長度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.