React PowerPlug is a set of pluggable renderless components to help you prototype quickly. These components don't actually render DOM to the page but contain the state and logic for you to power your presentation components via render props. In this lesson we'll look at the <State>
component and re-create the traditional counter example to see the potential of React PowerPlug.
Instructor: [00:00] Let's take a look at the infamous Counter example, first as a regular React component then as a PowerPlug component. As you can see here, I have a simple class component with a Counter for State and two handlers for incrementing and decrementing.
[00:13] The first component you'll find in the React PowerPlug library is the State component. All additional components in the library are built using the State component, and it's what we'll use to get started with our Refactor.
[00:24] The first thing we'll need to do is wrap our component with a State component and pass it in an initial value for our State. Remember, the React PowerPlug library doesn't render any DOM itself but acts more as a container for your State and logic that you can pass to your UI. It does this in the form of the Render props pattern which allows us to render whatever we want.
[00:43] This State PowerPlug component will give use two parameters we can use in our component -- State and Substate. All right, so let's got ahead and adjust our existing implementation to utilize the params provided in our render prop.
[00:57] This .state.counter can be replaced with just state.counter, and our event handlers can simply be in line and called a set State method directly. The main advantage to this pattern is the clean separation between State logic and the presentational component in charge of rendering the UI.
[01:13] The State component is agnostic at what it's rendering. It just supplies the UI component with the necessary tools to get its job done. All right, so that looks like it's working. Let's go ahead and clear out our existing component to clean it up a bit.
In the new
setState
calls, shouldn't we use the function form so nothing can go wrong with batching ofsetState
?
I meant to refactor this to be a functional component which cleans things up substantially and helps show the power of the wrapping State component. I haven't experienced an issue with conflicting setState
yet but you make a valid point!
^ Next lessons will utilize functional components + React PowerPlug
In the new
setState
calls, shouldn't we use the function form so nothing can go wrong with batching ofsetState
?