DEV Community

Discussion on: Functional Programming with Forms in React

Collapse
avikaminetzky profile image
Avi Kaminetzky

Interesting technique. I like how it DRY's up the code.

What are the benefits of insisting on forms being pure functions in this particular scenario?

Collapse
nestedsoftware profile image
Nested Software Author • Edited on

Hi @avremel ,

I think you may have answered your question, re. DRY. :) There are other benefits to pure functions in a more general sense, but I think here DRY more or less covers it: The design allows us to place all of our code that changes state and produces side-effects in one place. It means our forms become much simpler to write and understand.

I don't know if there is an insistence on making every form a pure function, but it seems to be a natural and beneficial aspect of the design.

Collapse
avikaminetzky profile image
Avi Kaminetzky • Edited on

Agreed.

I would just add that with this pattern you get all the benefits of stateless functional components.
See this article (he notes that pure functions become easier to reason about, in functional programming jargon, "referential transparency"):
hackernoon.com/react-stateless-fun...