npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

piral-redux

v1.5.5

Published

Plugin for the integration of Redux in a Piral instance.

Downloads

1,676

Readme

Piral Logo

Piral Redux · GitHub License npm version tested with jest Community Chat

This is a plugin that only has a peer dependency to piral-core. What piral-redux brings to the table is a set of Pilet API extensions that can be used with piral or piral-core for including a state container managed by Redux.

By default, these API extensions are not integrated in piral, so you'd need to add them to your Piral instance.

Why and When

Piral comes with an integrated state management. There is no need to use this for your own purposes, but you could use it. If you don't want to use it the chance that you actually want to use the popular redux library is quite high. Now, every pilet could come up with its own state management system, however, sharing this kind of library makes sense. Potentially what makes even more sense is having a single store, where all pilets would get a fraction of it. As such states could even be shared and the complexity of knowing what's happening in the application is reduced to monitoring a single store.

Alternatives: Use the integrated state management. Just expose redux and react-redux as shared dependencies.

Documentation

The following functions are brought to the Pilet API.

createReduxStore()

Creates a new Redux store for the pilet. The store is tighly coupled to the lifetime of the pilet.

This function returns the connect function known from React Redux - just applied to the pilet store.

Usage

::: summary: For pilet authors

Use the function createReduxStore to obtain a store connector. The store connector is a higher-order component that wraps an existing component and removes the state and dispatch props. Instead, state will be "connected" to the created pilet store and dispatch allows modifying the state by calling the reducer with the provided action.

Let's see a full example.

export function setup(api: PiletApi) {
  const connect = api.createReduxStore(myReducer);
  // ...
}

The reducer could be defined such as:

const initialState = {
  count: 0
};

function myReducer(state = initialState, action) {
  switch (action.type) {
    case "increment":
      return {
        count: state.count + 1
      };
    case "decrement":
      return {
        count: state.count - 1
      };
    default:
      return state;
  }
}

Using this construct is straight forward and follows other create... Pilet APIs.

root.registerPage(
  "/sample",
  connect(({ state, dispatch }) => (
    <div>
      <button onClick={() => dispatch({ type: 'increment' })}>Increment</button>
      <button onClick={() => dispatch({ type: 'decrement' })}>Decrement</button>
      {state.count}
    </div>
  ))
);

:::

::: summary: For Piral instance developers

The provided library only brings API extensions for pilets to a Piral instance.

For the setup of the library itself you'll need to import createReduxApi from the piral-redux package.

import { createReduxApi } from 'piral-redux';

The integration looks like:

const instance = createInstance({
  // important part
  plugins: [createReduxApi()],
  // ...
});

There are two options available. The reducer option allows us to define reducers that also access and manipulate the global state. The enhancer option allows us to pass in a custom store enhancer. For more details on enhancers please look at the Redux documentation.

:::

License

Piral is released using the MIT license. For more information see the license file.