Better State Management for React using Providence

Posted by Fox Piacenti on July 22, 2022

A few months back, I wrote a blog post on how to build your own Micro Front End for the Open edX platform. More than half of the post's content was centered around Redux. Redux is complicated. It's frustrating. In fact, most developers I know would rather forget it even exists. However, it's hard to let go of the advantages that Redux gives us. What if we could get all of its advantages, but have a much simpler interface? Enter the Eye of Providence State Management Library, or Providence for short. It gives you better state management for React without the hassle.

Why Use Libraries like Redux in the First Place?

If you've spent any amount of time developing with React, you've probably built something where you need to share state between multiple components at once. There are a few ways to handle this, but the traditional means is to built a Redux store. A Redux store allows you to store all of your state in one place.

Not only that, but it also allows you to replay the entire history of your state using debugging tools. You can rewind and fast-forward to see every way your data has changed since your app initialized! That's pretty neat.

It's exciting to have such power at your fingertips-- but developers are quickly hit with reality. Just getting Redux to work in the most minimal case requires an enormous amount of code, and it's mind-bending to wrap your head around. It's almost as if developers weren't meant to work directly with it.

Using Redux as a Storage Driver

That's because they weren't. The biggest mistake most teams make with Redux is treating it as a state management library. It makes sense-- Redux is sold as the prescribed state management library for React-based projects. However, using Redux for state management is the wrong level of abstraction. Redux's verbosity is needed to provide its guarantees. However, what is verbose can be automated.

Since Redux has to support virtually every state management need you can think of, it has to be very open ended. However, most of what we need to manage on the frontend boils down to:

  • Objects
  • Arrays of Objects
  • Managing the fetching and updating of these objects using an API
  • Forms

Providence handles these cases with ease, and lets you focus on development instead of fighting with Redux. Since it's built on top of Redux, you can still use the full power of its features when you need them.

How Providence Provides Better State Management for React

Providence provides better state management for React by autogenerating Redux store modules. It works with Microsoft's Redux Dynamic Modules library to add and remove sections of the state as components need them. Take, for example, this single line of Providence code:

const controller = useSingle('product', {endpoint: 'https://example.com/api/products/x/'})

What does this line get you?

  • A Redux Module with mutations
  • A controller that makes the state easy to update
  • Easy REST patching
  • Error handling for fetching and updating the object

An array is similar:

controller = useList('currentItem', {endpoint: 'https://example.com/api/endpoint/'})

With this array (called a 'List' in Providence parlance), you receive:

  • A set of Redux modules, one for each object you fetch from the endpoint
  • Pagination handling
  • Convenience methods for posting to a list to create a new item
  • Again, error handling!

A form is a bit more complicated, but not by much:

controller = useForm('survey', {
  endpoint: 'https://example.com/api/survey/',
  fields: {
    age: {value: 20},
    email: {value: '', validators: [{name: 'email'}]},
    name: {value: ''},
  }
})

With this form, you get:

  • Convenience "Fielders" that make authoring form components a breeze!
  • A straightforward system for front-end form validation
  • More error handling-- get errors per-field, and form-wide!

See Providence in Action

A screenshot from Listaflow
Listaflow is an in-development web application that uses Providence to get better state management for React

It may be difficult to wrap your head around the level of magic Providence provides. The best way to get a hold of it is to check out the demo, and then to read its code.

Providence comes with in-depth documentation, and we're currently using it to build a new project, Listaflow, which is open source, and currently being tested in limited beta for the Open edX Core Contributor sprint check ins.

Image courtesy Denny Müller via Unsplash

Latest Posts
June 6, 2024
Catch OpenCraft at the Open edX Conference 2024

This year, the Open edX Conference will be held from July 2 – 5, 2024 in Stellenbosch, South Africa. I’m […]

Read More
September 18, 2023
What is the Impact of AI in the Online Learning Space?

The arrival of new AI technology has sent the world of online education abuzz. The new technologies have brought new […]

Read More
July 3, 2023
June 2023: OpenCraft Quarterly Catch Up

Open edX presents Content Tagging! "Tagging" has been a long-requested feature for managing content in Studio, and now OpenCraft is finally designing and […]

Read More

We deliver top-notch learning solutions.

We’re an elite team of designers and developers, who love creating quality learning management solutions. Let’s chat about your latest project.
Get in touch

Get in touch

We'd love to hear from you! Send us a message and a member of our team will get back to you.
Contact us
Subscribe to our newsletter
© OpenCraft 2024
edX and Open edX® are trademarks of edX Inc
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram