Search code examples
javascriptredux

continious functions in javascript


I'm learning redux and in this piece of code section there is a continuous function to pass the arguments. I couldn't understand the logic behinds that because i'm the PHP developer originally and it's unusual there. would you please explain and simplify that for me?

Here is my selector:

export const personBugs = userId => createSelector(
    state => state.entities.bugs,
    bugs => bugs.filter(bug => bug.userId === userId )
)

And here is my dispatch:

console.log(bugsActions.personBugs(1)(store.getState()));

Solution

  • It's called a curry function or currying, this is when you want to provide one argument and at a later time provide another argument.

    I am not sure why a selector is on your actions object but I'm sure that is a mistake. A selector is usually used in components to get data that the component needs but can also be used in middleware to get state info.

    Here is an example of how a selector can be used in a component:

    const PersonBugs = React.memo(function PersonBugs({
      personId,
    }) {
      //here is a way to use the curry
      const bugs = useSelector(personBugs(personId));
      //if this wasn't a curry you have to do
      // const bugs = useSelector((state) =>
      //   personBugs(personId, state)
      // );
      //you could also not memoize the selector if you needed to
    });
    

    More information about selectors can be found here