Coder Social home page Coder Social logo

Comments (5)

CaptainCodeman avatar CaptainCodeman commented on September 26, 2024

Not sure what benefit variadic parameters add here, what's being passed exactly - an array of strings or an array of string arrays?

from rdx.

tpluscode avatar tpluscode commented on September 26, 2024

Array of strings. What benefit would you have in mind? I though this would allow calling the reducer with a variable number of arguments. reduce(foo), reduce(foo, bar), etc.

from rdx.

CaptainCodeman avatar CaptainCodeman commented on September 26, 2024

That's not really a benefit - the intent is that each reducer function does a single type of update, handling a single action, not that it acts as a top level reducer expecting varying types of action which it then has to inspect to decide how to handle (that's the Redux way of working).

What you likely need are separate functions for each distinct action and they will then provide the dispatch functions to use, again, with the parameters specific to each.

A parameter could still be optional in theory but I can't think of any use-case that makes sense off-hand.

from rdx.

tpluscode avatar tpluscode commented on September 26, 2024

I think you misunderstood my intent. Here's a little more tangible example, simple reducer to append a varying number of items to a collection

function push(state, ...newItems: string[]) {
  return {
    ...state,
    items: [ ...state.items, ...newItems ],
  }
}

I check now that it actually does not work and only the first argument passed to dispatch is forwarded to the reducer.

On the other hand an optional parameter, while I have not had such a need yet, could make sense indeed and also suffers a similar issue with the inferred type for the dispatcher interface. Off the top of my head would be something like dispatch.loadingFailed(error) where the error is possibly undefined. That, however, can be handler by typing the reducer just slightly differently

-loadingFailed(state: State, error?: Error)
+loadingFailed(state: State, error: Error | undefined)

This works unless one would desire to call this without parameter indeed.

from rdx.

CaptainCodeman avatar CaptainCodeman commented on September 26, 2024

You could use the following to achieve what you want:

push(state, items: string[]) {
  return {
    ...state,
    items: [ ...state.items, ...items ],
  }
}

from rdx.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.