Convert Figma logo to code with AI

renatorib logoreact-powerplug

:electric_plug: Renderless Containers

2,682
100
2,682
12

Top Related Projects

34,040

Build forms in React, without the tears 😭

🏁 High performance subscription-based form state management for React

42,148

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.

The official, opinionated, batteries-included toolset for efficient Redux development

27,618

Simple, scalable state management.

48,653

🐻 Bear necessities for state management in React

Quick Overview

The react-powerplug library is a collection of reusable React components that provide common state management and UI functionality. It aims to simplify the development of complex React applications by encapsulating common patterns and behaviors into easy-to-use components.

Pros

  • Reusable Components: The library provides a set of pre-built components that handle common state management and UI tasks, reducing the need for custom implementation.
  • Flexible and Composable: The components are designed to be highly customizable and can be easily composed together to build complex UIs.
  • Lightweight: The library is relatively small in size, with a focus on providing only the essential functionality.
  • Actively Maintained: The project is actively maintained, with regular updates and bug fixes.

Cons

  • Limited Functionality: While the library provides a good set of components, it may not cover all the use cases that developers might encounter, requiring additional custom implementation.
  • Learning Curve: Developers new to the library may need to invest time in understanding the component API and how to effectively use them in their projects.
  • Dependency on React: The library is tightly coupled with React, and developers who are not using React may not be able to benefit from it.
  • Potential Performance Overhead: Depending on the complexity of the components used, there may be some performance overhead compared to custom implementations.

Code Examples

Render Props Example

import { Toggle } from 'react-powerplug';

const MyComponent = () => (
  <Toggle>
    {({ on, toggle }) => (
      <div>
        <button onClick={toggle}>Toggle</button>
        {on && <p>The button is on!</p>}
      </div>
    )}
  </Toggle>
);

This example demonstrates the use of the Toggle component from react-powerplug. The component manages the state of a toggle button, and the render prop function allows you to access the on state and toggle function to control the button's behavior.

Lifecycle Example

import { Lifecycle } from 'react-powerplug';

const MyComponent = () => (
  <Lifecycle
    onMount={() => console.log('Component mounted!')}
    onUnmount={() => console.log('Component unmounted!')}
  >
    {() => <div>My component</div>}
  </Lifecycle>
);

This example shows the use of the Lifecycle component, which allows you to easily handle component lifecycle events, such as mounting and unmounting, without having to manage the lifecycle methods directly.

State Example

import { State } from 'react-powerplug';

const MyComponent = () => (
  <State initial={{ count: 0 }}>
    {({ state, setState }) => (
      <div>
        <p>Count: {state.count}</p>
        <button onClick={() => setState((prevState) => ({ count: prevState.count + 1 }))}>
          Increment
        </button>
      </div>
    )}
  </State>
);

This example demonstrates the use of the State component, which provides a simple way to manage component state without having to write custom state management logic.

Getting Started

To get started with react-powerplug, you can install the library using npm or yarn:

npm install react-powerplug

or

yarn add react-powerplug

Once installed, you can start using the provided components in your React application. Here's a basic example of how to use the Toggle component:

import React from 'react';
import { Toggle } from 'react-powerplug';

const MyComponent = () => (
  <Toggle>
    {({ on, toggle }) => (
      <div>
        <button onClick={toggle}>Toggle</button>
        {on && <p>The button is on!</p>}
      </div>
    )}
  </Toggle>
);

export default MyComponent;

In this example, the Toggle component manages the state of a toggle button, and the render prop function allows you to access the on state and toggle function to control the button's behavior.

You can explore the other components provided by `react-power

Competitor Comparisons

34,040

Build forms in React, without the tears 😭

Pros of Formik

  • Specifically designed for form handling, offering a more comprehensive solution for form-related tasks
  • Larger community and more frequent updates, providing better support and maintenance
  • Built-in validation support, reducing the need for additional libraries

Cons of Formik

  • More opinionated and less flexible for non-form related state management
  • Steeper learning curve due to its form-specific API and concepts
  • Potentially overkill for simple form scenarios or projects with minimal form requirements

Code Comparison

Formik:

import { Formik, Form, Field } from 'formik';

<Formik initialValues={{ name: '' }} onSubmit={handleSubmit}>
  <Form>
    <Field name="name" />
    <button type="submit">Submit</button>
  </Form>
</Formik>

React PowerPlug:

import { State } from 'react-powerplug';

<State initial={{ name: '' }}>
  {({ state, setState }) => (
    <form onSubmit={handleSubmit}>
      <input value={state.name} onChange={e => setState({ name: e.target.value })} />
      <button type="submit">Submit</button>
    </form>
  )}
</State>

While Formik provides a more form-centric approach with built-in components and handlers, React PowerPlug offers a more generic state management solution that can be adapted for various use cases, including forms.

🏁 High performance subscription-based form state management for React

Pros of react-final-form

  • More comprehensive form management solution with built-in validation and error handling
  • Better performance optimization for large forms with many fields
  • Extensive documentation and active community support

Cons of react-final-form

  • Steeper learning curve due to more complex API
  • Heavier bundle size compared to the lightweight react-powerplug
  • Less flexibility for custom state management outside of forms

Code Comparison

react-final-form:

import { Form, Field } from 'react-final-form'

<Form
  onSubmit={onSubmit}
  render={({ handleSubmit }) => (
    <form onSubmit={handleSubmit}>
      <Field name="firstName" component="input" placeholder="First Name" />
      <button type="submit">Submit</button>
    </form>
  )}
/>

react-powerplug:

import { State } from 'react-powerplug'

<State initial={{ firstName: '' }}>
  {({ state, setState }) => (
    <form onSubmit={handleSubmit}>
      <input
        value={state.firstName}
        onChange={e => setState({ firstName: e.target.value })}
        placeholder="First Name"
      />
      <button type="submit">Submit</button>
    </form>
  )}
</State>

react-final-form is more suited for complex form management, while react-powerplug offers a simpler, more flexible approach to general state management in React components.

42,148

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.

Pros of TanStack Query

  • Robust data fetching and caching solution with advanced features like automatic refetching and background updates
  • Supports various data sources including REST APIs, GraphQL, and more
  • Large and active community with extensive documentation and ecosystem

Cons of TanStack Query

  • Steeper learning curve due to its comprehensive feature set
  • May be overkill for simpler applications or those with minimal data fetching needs
  • Requires more setup and configuration compared to React PowerPlug

Code Comparison

React PowerPlug (State management):

import { State } from 'react-powerplug'

<State initial={{ count: 0 }}>
  {({ state, setState }) => (
    <div>
      <span>{state.count}</span>
      <button onClick={() => setState({ count: state.count + 1 })}>+</button>
    </div>
  )}
</State>

TanStack Query (Data fetching):

import { useQuery } from '@tanstack/react-query'

function Example() {
  const { data, isLoading } = useQuery(['todos'], fetchTodos)
  if (isLoading) return 'Loading...'
  return <div>{data.map(todo => <Todo key={todo.id} {...todo} />)}</div>
}

Summary

TanStack Query excels in complex data fetching scenarios, offering powerful caching and synchronization features. React PowerPlug, on the other hand, provides simpler state management utilities. Choose TanStack Query for data-heavy applications, and React PowerPlug for lightweight state management needs.

The official, opinionated, batteries-included toolset for efficient Redux development

Pros of Redux Toolkit

  • Provides official, opinionated toolset for efficient Redux development
  • Includes utilities to simplify common Redux use cases (e.g., createSlice)
  • Integrates well with existing Redux ecosystems and patterns

Cons of Redux Toolkit

  • Steeper learning curve for developers new to Redux concepts
  • More boilerplate code compared to React PowerPlug's simpler approach
  • Requires additional setup and configuration

Code Comparison

Redux Toolkit:

import { createSlice } from '@reduxjs/toolkit'

const counterSlice = createSlice({
  name: 'counter',
  initialState: 0,
  reducers: {
    increment: state => state + 1,
  },
})

React PowerPlug:

import { Value } from 'react-powerplug'

<Value initial={0}>
  {({ value, set }) => (
    <button onClick={() => set(value + 1)}>
      {value}
    </button>
  )}
</Value>

Summary

Redux Toolkit offers a comprehensive solution for managing complex state in large applications, while React PowerPlug provides a simpler, more lightweight approach for basic state management needs. Redux Toolkit is better suited for scalable applications with intricate state logic, whereas React PowerPlug excels in rapid prototyping and smaller projects requiring quick setup.

27,618

Simple, scalable state management.

Pros of MobX

  • More comprehensive state management solution with automatic tracking of observables
  • Supports complex state structures and derived values (computed properties)
  • Better performance for large-scale applications with many state updates

Cons of MobX

  • Steeper learning curve due to its more complex API and concepts
  • Requires decorators or configuration for optimal usage, which may not be ideal for all projects
  • More opinionated approach to state management, potentially limiting flexibility

Code Comparison

MobX:

import { makeObservable, observable, action } from "mobx";

class Counter {
  count = 0;
  constructor() {
    makeObservable(this, {
      count: observable,
      increment: action
    });
  }
  increment() {
    this.count++;
  }
}

React-powerplug:

import { State } from 'react-powerplug';

const Counter = () => (
  <State initial={{ count: 0 }}>
    {({ state, setState }) => (
      <button onClick={() => setState({ count: state.count + 1 })}>
        {state.count}
      </button>
    )}
  </State>
);

React-powerplug provides a simpler, more declarative approach to state management within React components, while MobX offers a more powerful and scalable solution for complex state management needs across an entire application.

48,653

🐻 Bear necessities for state management in React

Pros of zustand

  • Simpler API with less boilerplate code
  • Better performance due to its minimalistic approach
  • Supports middleware and devtools out of the box

Cons of zustand

  • Less flexibility for complex state management scenarios
  • Fewer built-in utilities compared to react-powerplug

Code Comparison

react-powerplug:

import { State } from 'react-powerplug'

<State initial={{ count: 0 }}>
  {({ state, setState }) => (
    <div>
      <span>{state.count}</span>
      <button onClick={() => setState(s => ({ count: s.count + 1 }))}>+</button>
    </div>
  )}
</State>

zustand:

import create from 'zustand'

const useStore = create(set => ({
  count: 0,
  increment: () => set(state => ({ count: state.count + 1 })),
}))

function Counter() {
  const { count, increment } = useStore()
  return <button onClick={increment}>{count}</button>
}

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

React PowerPlug

npm stars tweet


React PowerPlug is a set of pluggable renderless components and helpers that provides different types of state and logic utilities that you can use with your dumb components. It creates state and passes down the logic to the children, so you can handle your data. Read about the Render Props pattern.

Highlights

  • :ok_hand: Dependency free
  • :electric_plug: Plug and play
  • :crystal_ball: Tree shaking friendly (ESM, no side effects)
  • :package: Super tiny (~3kb)
  • :books: Well documented
  • :beers: Bunch of awesome utilities
See quick examples
import { State, Toggle } from 'react-powerplug'
import { Pagination, Tabs, Checkbox } from './MyDumbComponents'

<State initial={{ offset: 0, limit: 10, totalCount: 200 }}>
  {({ state, setState }) => (
    <Pagination {...state} onChange={(offset) => setState({ offset })} />
  )}
</State>

<Toggle initial={true}>
  {({ on, toggle }) => (
    <Checkbox checked={on} onChange={toggle} />
  )}
</Toggle>

// You can also use a `render` prop instead

<Toggle
  initial={false}
  render={({ on, toggle }) => (
    <Checkbox checked={on} onChange={toggle} />
  )}
/>

Guide & Documentation

http://rena.to/react-powerplug/


Watch 'Rapid Prototyping with React PowerPlug' by Andrew Del Prete on egghead.io


Install

Node Module

yarn add react-powerplug
npm i react-powerplug

UMD

<script src="https://unpkg.com/react-powerplug/dist/react-powerplug.min.js"></script>

exposed as ReactPowerPlug

Contributors

Thanks goes to these wonderful people (emoji key):


Renato Ribeiro

💻 🎨 📖 ⚠️

Bogdan Chadkin

💻 📖 ⚠️ 🚇

Travis Arnold

💻 📖 🐛

Max Graey

💻

Mateusz Burzyński

🐛

Andy Edwards

💻

Andrea Vanini

🐛

Ivan Starkov

🐛

Sean Roberts

📖

Braden Kelley

🐛

This project follows the all-contributors specification. Contributions of any kind welcome!

Contribute

You can help improving this project sending PRs and helping with issues.
Also you can ping me at Twitter

NPM DownloadsLast 30 Days