0

Was looking at examples on reselect's createSelector at https://react-redux.js.org/next/api/hooks#using-memoizing-selectors.

What is the _ in (_,isDone) => isDone?

Is it lodash? How did it get injected? Was it injected by the library?

import React, { useMemo } from 'react' import { useSelector } from 'react-redux' import { createSelector } from 'reselect' const makeNumOfTodosWithIsDoneSelector = () => createSelector( state => state.todos, (_, isDone) => isDone, (todos, isDone) => todos.filter(todo => todo.isDone === isDone).length ) export const TodoCounterForIsDoneValue = ({ isDone }) => { const selectNumOfTodosWithIsDone = useMemo( makeNumOfTodosWithIsDoneSelector, [] ) const numOfTodosWithIsDoneValue = useSelector(state => selectNumOfTodosWithIsDone(state, isDone) ) return <div>{numOfTodosWithIsDoneValue}</div> } export const App = () => { return ( <> <span>Number of done todos:</span> <TodoCounterForIsDoneValue isDone={true} /> <span>Number of unfinished todos:</span> <TodoCounterForIsDoneValue isDone={false} /> </> ) } 
2
  • 5
    Is just an unused parameter, since the code is only reading from the second parameter, naming the first would throw a lint warning for unused variables. Naming as _ will prevent the behavior. Take a look at this answer Commented Dec 9, 2019 at 17:55
  • Thanks. I guess that's how reselect works then :). Commented Dec 11, 2019 at 6:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.