# Simplify Immer Producer Functions using Currying

## Social Share Links

Published 5 years ago
Updated 3 years ago

Once we’ve written a bunch of producer functions, we quickly discover a recurring pattern: In practice producers can be defined purely in terms of the draft (and any remaining arguments). Therefor we can factor out the function nesting by leveraging currying. We will also look at the “original” function.

Instructor: [00:00] Let's take another look at the logic we have so far. These actions pass all the tests, but there is also a recurring pattern. We write functions that receive the state and a bunch of additional arguments, then we simply return a call produce, which we pass the very same state.

[00:17] Then we have to draft. We're just repeating this pattern over and over again. Can we simplify this? Yes, we can simplify this. We can use the fact that the produce function also supports currying. So far, we're calling produce always like produce, state, and some recipe. Then it returns the next state.

[00:40] However, we can use produce in a different way. We can use produce in such a way that we only pass a recipe function, and then returns us a function that accepts a state, and then produces a next state according to the recipe we pass in over here.

[00:57] Let's rewrite this a little bit. These are still the same. We just use a different syntax to define these functions. Now we can reproduce simply around the other function. It runs function that accepts the state, and then produces a next state according to the recipe we pass in over here.

[01:18] Let's do the same with toggle reservation. We're going to eliminate the state from this function and define it just in terms of the draft. Over here, we're looking into the current state, but let's find that very same information from the draft. We rep this one and produce as well. We save it, and still our tests are passing.

[01:38] Now that's in a function that is produced by produce which accepts the states. All additional arguments, they're just literally passed onto the recipe so that they're available in the recipe as well. This really simplifies writing producers by just defining them in terms of the draft.

[01:58] In Immer terminology, a recipe function, that is reps with produce, is called producer.

[02:05] Finally, you might be wondering, what if I need to access the original state? That is actually quite easy. For that, there's a small utility function in Immer that's called original. Theoretically, we could simply say, "Well, what was the state of the current user before this producer started by using original?"

[02:25] In this case, it doesn't change the behavior. Just be aware it is there if you want to take a look at the original function.

~ 2 hours ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Avoid meta-discussion

• This was great!
• This was horrible!
• I didn't like this because it didn't match my skill level.
• +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today