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

flux-native

v1.0.70

Published

Simple, safe flux library for React

Downloads

85

Readme

flux-native

Features

  1. Easy to use and simple APIs, no reducer, no provider, no action type, no 3rd lib needed for async dispatching
  2. Support advance action handling: cancel, suspend, resume
  3. Support action transaction
  4. Support state merging
  5. Support multiple action dispatching ways: once, on component mount, on component unmount

Counter App

import React from "react";
import { render } from "react-dom";
import { dispatch, useStore } from "flux-native";

const Initial = () => ({ count: 1 });

dispatch(Initial);

const Increase = state => ({ ...state, count: state.count + 1 });

const App = () => {
  const [count] = useStore(state => state.count);
  const handleIncrease = () => dispatch(Increase);
  return (
    <>
      <h1>{count}</h1>
      <button onClick={handleIncrease}>Increase</button>
    </>
  );
};

render(<App />, document.getElementById("root"));

Search App

import React from "react";
import { render } from "react-dom";
import { dispatch, useStore } from "flux-native";

const Initial = () => ({ keyword: "", results: [] });

dispatch(Initial);

const Fetch3Items = state => ({
  ...state,
  results: ["result 1", "result 2", "result 3"]
});

const NotFound = state => ({ ...state, results: [] });

// save keyword to sate
const UpdateKeyword = (state, keyword) => ({ ...state, keyword });

const FetchSearchResults = state => ({ dispatch, check }) => {
  // enable state checking before dispatching
  check((current, prev) => {
    // perform dispatching if keyword does not change
    return current.keyword === prev.keyword;
  });

  if (state.keyword === "abc") {
    setTimeout(dispatch, 1000, Fetch3Items);
  } else {
    dispatch(NotFound);
  }
};

const Search = (state, keyword) => ({ dispatch }) => {
  dispatch(UpdateKeyword, keyword);
  dispatch(FetchSearchResults);
};

const App = () => {
  const [results] = useStore(state => state.results);
  const handleSearch = e => dispatch(Search, e.target.value);
  return (
    <>
      <input
        type="text"
        onChange={handleSearch}
        placeholder="Enter search term (ex: abc)"
      />
      <ul>
        {results.map(result => (
          <li key={result}>{result}</li>
        ))}
      </ul>
    </>
  );
};

render(<App />, document.getElementById("root"));

Monitoring and handling action dispatching status

import React, { useRef, useState } from "react";
import { render } from "react-dom";
import { dispatch, useStore } from "flux-native";

const LoadData = () => ({ merge, onCancel }) => {
  // mark data is loading
  merge({ loading: true });

  onCancel(() => {
    // mark nothing to load
    merge({ loading: false, data: undefined });
  });

  // assign test data after 2s
  setTimeout(merge, 2000, {
    loading: false,
    data: "Data loaded " + Math.random().toString(16)
  });
};

const App = () => {
  const [data, loading] = useStore(state => state.data, state => state.loading);
  const loadDataHandler = useRef();
  const [, forceRerender] = useState();
  const handleStatusChanged = () => {
    forceRerender({});
  };
  const handleLoad = () => {
    loadDataHandler.current = dispatch(LoadData);
    // register status changed listener
    loadDataHandler.current.onStatusChange(handleStatusChanged);
  };
  const handleCancel = () => {
    loadDataHandler.current && loadDataHandler.current.cancel();
  };
  const handleSuspend = () => {
    loadDataHandler.current && loadDataHandler.current.suspend();
  };
  const handleResume = () => {
    loadDataHandler.current && loadDataHandler.current.resume();
  };
  return (
    <>
      <p>
        <button onClick={handleLoad}>Load data</button>
        {loading && (
          <>
            <button onClick={handleCancel}>Cancel</button>
            {loadDataHandler.current.suspended() ? (
              <button onClick={handleResume}>Resume</button>
            ) : (
              <button onClick={handleSuspend}>Suspend</button>
            )}
          </>
        )}
      </p>
      <p>
        {loadDataHandler.current && loadDataHandler.current.suspended()
          ? "Suspended"
          : loading
          ? "Loading..."
          : data}
      </p>
    </>
  );
};

render(<App />, document.getElementById("root"));

Dispatching action once

import { dispatchOnce } from "flux-native";

const Initialize = () => ({ data: [] });

dispatchOnce(Initialize);

Dispatching action when component mount or unmount

import React from "react";
import { dispatchOnMount, dispatchOnUnmount } from "flux-native";

const OnComponentMount = (state, ...args) => {
  console.log("component mount", args);
};
const OnComponentUnmount = (state, ...args) => {
  console.log("component unmount", args);
};

const MyComponent = () => {
  dispatchOnMount(OnComponentMount, [1, 2, 3], ["deps"]);
  dispatchOnUnmount(OnComponentUnmount, [1, 2, 3], ["deps"]);
  return <div />;
};

Handle state change

import { getState, subscribe } from "flux-native";

const unsubscribe = subscribe(() => {
  console.log('state changed', getState());
});

// unsubscribe()