If you’re trying to rapid prototype an app the last thing you want to be doing is implementing the same state management logic over and over. Adding something like Redux can help but tends to just adds a layer of complexity that can slow you down every further. React PowerPlug makes rapid prototyping a breeze by introducing a set of stateful components that let you focus on the good stuff, actually prototyping!
React PowerPlug is a set of renderless components that provide different types of state management scenarios by way of render props. The project is dependency-free, well documented and pretty small at around 3kb.
A word of warning though, the project’s master
branch is still considered unstable and under active development. I opted to talk about the unstable version because it has so much more to offer in terms of different types of stateful components.
To get things started, we will need to add React PowerPlug to our project:
With the dependency added to our project, we will need to import React PowerPlug in it’s entirety:
Or import the individual components we’d like to use:
As mentioned, the master
branch of this project has a ton of additional stateful components.
While the type of data may be different between the components, nearly all of the components accept an initial
property to set the default state.
A component’s state can come in many different forms. It could be as simple as holding a single value or as complex as a mixed bag of boolean values, counters and string values.
State
is one of the more basic components. Very similar to React’s baked in state
property, State
allows you to maintain an object of state properties that can updated via setState
:
Toggle
is a component for maintaining the state of a boolean value:
Counter
allows you to increment and decrement an integer in the state:
Value
is for maintaining the state of a single value. Set it and forget it:
The Map
component is quite similar to State
as it controls state as an object with different properties. Where it differs is that you interact with the state via get
and set
methods:
Not to be confused with the aforementioned set
method, the Set
component manages it’s state as an array of values which you can add
to and remove
from:
List
also holds it’s state as an array. Instead of simple add
and remove
methods, you interact with the array via push
and pull
methods.
Considering the complexity that is introduced by needing to know the index of the array item when pull
ing from the state, I’d probably just stick to Set
:
Keeping track of a user’s interaction with a component usually includes binding event handlers on top of keeping track of the current state. React PowerPlug does a great job of not only combining these implementations but also keeping you fairly insulated from needing to worry about event handlers.
Hover
keeps track of whether or not a user is hovering over a component:
Active
knows if a user is clicking on a component:
Similar to Active
, the Touch
component is the touch-friendly equivalent:
Focus
is perfect for showing and hiding information based on which field a user is currently interacting with:
Even though React PowerPlug has components that could easily be used to wrap up form components, they still took the time to include some form-specific components to help save you time:
Input
, which works with input
instead of replacing it, binds input events to an input
or any form field and stashes the value in the state:
The Form
component takes things a step further by allowing you to track the state of multiple fields on a form with ease:
React PowerPlug isn’t just for tracking state variables and user input, you can also use it wire up components to update automatically.
Unlike the other components we’ve discussed, Interval
doesn’t take an initial
state value and instead takes delay
(in milliseconds).
React PowerPlug stands up to the claims that it makes it easy to rapid prototype apps in React. As the project is very much a work in progress right now, I’m super excited to see where the team ends up taking it!
I hope that you enjoyed this run down of React PowerPlug and if you are interested in seeing the code samples in action, you can head over to CodeSandbox.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!