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} />
    </>
  )
}
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
hackerl33t
  • 2,117
  • 3
  • 14
  • 21
  • 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](https://stackoverflow.com/questions/11406823/underscore-as-a-javascript-variable) – Dupocas Dec 09 '19 at 17:55
  • Thanks. I guess that's how `reselect` works then :). – hackerl33t Dec 11 '19 at 06:20

0 Answers0