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

react-appevent-redux

v1.0.4

Published

React-Redux but event driven.

Downloads

3

Readme

Start development

MacOS

Run once:

./initenv.bash

Run this at start of terminal:

source ./devenv.bash

How to Use

Copy the content of the ./templates directory to your ./src directory and overwrite existing files. Or see below to configure manually.

Store configuration

Create a store-config.ts file in the project directory:

import { AppStore } from "react-appevent-redux";
import * as Redux from "@reduxjs/toolkit";
import { RootAppState } from "./states/RootAppState";

function rootReducer(state: RootAppState | undefined, action: Redux.AnyAction) {
  return state ?? new RootAppState({});
}

export const reduxStore = Redux.configureStore({
  preloadedState: new RootAppState({}),
  reducer: AppStore.wrapReducer(rootReducer),
  middleware: (getDefaultMiddleware) =>
    // react-appevent-redux uses classes to represent states
    getDefaultMiddleware({
      serializableCheck: false,
    }),
});

export const appStore = new AppStore<RootAppState>({
  reduxStore: reduxStore,
});

AppState Example

Create a states directory and create states/RootAppState.ts:

import { AppState, PartialProps } from "react-appevent-redux";
import { HomeAppState } from "./HomeAppState";

export class RootAppState extends AppState {
  home = new HomeAppState({});

  constructor(props: PartialProps<RootAppState>) {
    super();
    this.assignProps(props);
  }
}

Create states/HomeAppState.ts:

import { AppState, PartialProps } from "react-appevent-redux";

export class HomeAppState extends AppState {
  welcomeText = "Welcome!";
  constructor(props: PartialProps<HomeAppState>) {
    super();
    this.assignProps(props);
  }
}

HomePage Example

Create a connected-components directory and connected-components/HomePage.tsx:

import React from "react";
import { connect } from "react-redux";
import { HomeAppState } from "../states/HomeAppState";
import { RootAppState } from "../states/RootAppState";

type _State = {
  };

  type _Props = {
    homeAppState: HomeAppState;
  };

  class _Home extends React.Component<_Props, _State> {
    constructor(props: _Props) {
      super(props);
      this.state = {};
    }

    render() {
      return (
        <React.Fragment>
            <div>{this.props.homeAppState.welcomeText}</div>
        </React.Fragment>
      );
    }
  }

  export const HomePage = connect<_Props, {}, {}, RootAppState>((rootAppState: RootAppState) => ({
    homeAppState: rootAppState.home,
  }))(_Home);

Add the HomePage component to App.tsx:

import { HomePage } from "./connected-components/HomePage";

function App() {
  return (
    <HomePage></HomePage>
  );
}

export default App;

Initialize redux in index.tsx:

import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { Provider } from "react-redux";
import { reduxStore } from "./store-config";
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root')!);
root.render(
  <Provider store={reduxStore}>
      <App />
  </Provider>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

Build Static Page

Run npm start:

alt text

Handle Actions

AppEvent Example

Create a events directory and events/OnClickEvent.ts:

import { HomeAppState } from "../states/HomeAppState";
import { RootAppState } from "../states/RootAppState";
import { AppEvent, AppEventStream, mapState, to } from "react-appevent-redux";

export class OnClickEvent extends AppEvent<RootAppState> {
    constructor() {
      super();
    }

  reducer(state: RootAppState): RootAppState {
      return state.mapState({
        home: (home: HomeAppState) => home.mapState({
          welcomeText: (currentText: string) => "Clicked!",
        }),
      });
  }

  /*
  Or, use mapState(...) and to(...) helpers
    to make it more concise:

  reducer = mapState<RootAppState>({
    home: mapState<HomeAppState>({
      welcomeText: to("Clicked!"),
    }),
  });
  */

  async *run(state: RootAppState): AppEventStream<RootAppState> {}
}

Dispatch Event

Create a button in HomePage.tsx and when clicked, dispatch the OnClickEvent:

import React from "react";
import { connect } from "react-redux";
import { OnClickEvent } from "../events/OnClickEvent";
import { HomeAppState } from "../states/HomeAppState";
import { RootAppState } from "../states/RootAppState";
import { appStore } from "../store-config";

type _State = {
  };

  type _Props = {
    homeAppState: HomeAppState;
  };

  class _Home extends React.Component<_Props, _State> {
    constructor(props: _Props) {
      super(props);
      this.state = {};
    }

    render() {
      return (
        <React.Fragment>
            <div>{this.props.homeAppState.welcomeText}</div>
            <button onClick={() => appStore.dispatch(new OnClickEvent())}>Dispatch OnClickEvent</button>
        </React.Fragment>
      );
    }
  }

  export const HomePage = connect<_Props, {}, {}, RootAppState>((rootAppState: RootAppState) => ({
    homeAppState: rootAppState.home,
  }))(_Home);

after2