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-flux-context

v1.0.1

Published

a global store is created by context hook with flux pattern for react apps

Downloads

2

Readme

react-flux-context

A global store is created by context hook with flux pattern for react apps.

bundle size:

  • unpackage size: 4.4KB
  • gzip size: 1.6KB

You can structure your store by reducers, actions, types like a redux pattern, but it has a easier setup to use it and very small size.

(Do not use it in ssr, its not ready yet)!

And it has a redux-thunk and redux-persist usage too with very simple config.

List

Installation

with npm:

npm install --save react-flux-context

with yarn:

yarn add react-flux-context

go to List

store folder structure

First, create a store folder in your src folder in your project directory and then create three folder with the names actions, reducers and types in store folder.

types:

It shows user's action type and ux(user experience like loading for api request, ... ) in actions.

In your types folder, you create a file according to your requirements

export const REGISTER_REQUEST = "REGISTER_REQUEST";

export const REGISTER_SUCCESS = "REGISTER_SUCCESS";

export const REGISTER_FAILURE = "REGISTER_FAILURE";

go to List

reducers

In reducers files, set your initial state and reducer which is relevant to your action types

import {
  REGISTER_REQUEST,
  REGISTER_SUCCESS,
  REGISTER_FAILURE,
} from "../types/authTypes";

export const authState = {
  role: null,
  token: null,
  isLogin: false,
  loading: false,
};

export const authReducer = (state, { type, payload }) => {
  switch (type) {
    case REGISTER_REQUEST:
      return {
        ...state,
        loading: true,
      };

    case REGISTER_SUCCESS:
      return {
        ...payload,
        isLogin: true,
        loading: false,
      };

    case REGISTER_FAILURE:
      return {
        ...payload,
        loading: false,
      };

    default:
      throw new Error("there is no type for authReducers");
  }
};

go to List

actions

Action is to handle users actions.

Pattern in react-flux-context is a little different. when you want to create actions in redux, type keyname must be exist, but in this package you have to set key keyname too.

The reason is in the react-flux-context that we use selector pattern for better perfomance instead of using combination pattern for reducers! It means for example, when you put reducer in user keyword in your store collection, you have to set key in your dispatch where you set your reducer in collection like auth, user or , ... etc. Then you have to set dispatch argument in your actions as topest and first argument in your function.

If you want to give data to your reducers, set a payload as keyword. (like a fetching data from api and saving in store)

(This action pattern works like a redux-thunk)

import {
  REGISTER_REQUEST,
  REGISTER_SUCCESS,
  REGISTER_FAILURE,
} from "../types/authTypes";
import { loginApi } from "../../services";

// Login actions
const authRequsetAction = (dispatch) => {
  dispatch({ key: "auth", type: REGISTER_REQUEST });
};

const authSuccessAction = (dispatch, payload) => {
  dispatch({ key: "auth", type: REGISTER_SUCCESS, payload });
};

const authFailureAction = (dispatch) => {
  dispatch({ key: "auth", type: REGISTER_FAILURE });
};

export const authLoginAction = (dispatch) => {
  authRequsetAction(dispatch);

  loginApi()
    .then((res) => {
      authSuccessAction(dispatch, res);
    })
    .catch((err) => {
      authFailureAction(dispatch);
    });
};

go to List

create collection

To initialize your store, you must create a store collection and put it in FluxContextProvider store as a prop.

If you want to persist your data, set a storage type and storageKey

storage: local (localStorage) session (sessionStorage)

storageKey: "some text"

import { FluxContextProvider } from "react-flux-context";

import { authState, authReducer } from "./reducers/authReducer";
import { userState, usersReducer } from "./reducers/usersReducer";

const storeCollection = {
  user: {
    state: userState,
    reducer: usersReducer,
  },
  auth: {
    // persist your data in storage automatically
    // it works like a redux-persist but has a very easier config
    state: authState,
    reducer: authReducer,
    storage: "local", // local | session
    storageKey: "_myAuthKey_",
  },
};

export default function ContextProvider({ children }) {
  return (
    <FluxContextProvider store={storeCollection}>
      {children}
    </FluxContextProvider>
  );
}

go to List

useSelector

Select your state with callback function or set nulish data to get states.

import { useSelector } from "react-flux-context";

function Component() {
  const userData = useSelector((data) => data.user);
  // or
  const { user } = useSelector();

  return (
    <div>
      <p> user fullname: {userData.fullname} </p>
      <p> user fullname: {user.fullname} </p>
    </div>
  );
}

go to List

useDispatch

useDispatch get actions in param and then dispather is ready to be used.

import { useDispatch } from "react-flux-context";
import { userAction } from "../store/actions/userAction";

function Component() {
  const userDispath = useDispatch(userAction);

  useEffect(() => {
    userDispath();
  }, []);

  return (
    <div>
      // or you can use with click event
      <button onClick={() => userDispath()}>get user</button>
    </div>
  );
}

go to List

useMultiDispatch

You can use multiple actions with useMultiDispatch

import { useMultiDispatch } from "react-flux-context";
import { userAction } from "../store/actions/userAction";
import { logoutAction } from "../store/actions/authAction";

function Component() {
  const [userDispath, logoutAction] = useMultiDispatch([
    userAction,
    logoutAction,
  ]);

  useEffect(() => {
    userDispath();
  }, []);

  return (
    <div>
      // or you can use with onClick and other event!
      <button onClick={() => logoutAction()}>get user</button>
    </div>
  );
}

go to List

useFluxContext

First argument is like a useSelector. Second argument is like a useDispatch .

import { useMultiDispatch } from "react-flux-context";
import { userAction } from "../store/actions/userAction";

function Component() {
  const [userData, userDispatch] = useFluxMultiContext(
    (data) => data.user,
    userAction
  );

  return <> ... </>;
}

go to List

useFluxMultiContext

First argument is like a useSelector. Second argument is like a useMultiDispatch .

import { useMultiDispatch } from "react-flux-context";
import { logoutAction } from "../store/actions/authAction";
import { userAction } from "../store/actions/userAction";

function Component() {
  const [{ user, auth }, [logoutDispatch, userDispatch]] = useFluxMultiContext(
    null,
    [logoutAction, userAction]
  );

  return <> ... </>;
}

go to List

useRefStore

You can use it for binary stream data in webrtc project or styling for motions, ... .

useRefStore(initialRefStore, isFunction = false)

You can store a function in useRefStore to use it anywhere

useRefStore(() => console.log('hi'), true)

import { useRef } from "react";
import { useRefStore } from "react-flux-context";

function Component() {
  const elmRef = useRef();
  const [myRef, setMyRef] = useRefStore(0);

  const moveForward = () => {
    setMyRef(myRef.current + 10);
    ref.current.style.top = myRef.current;
  };

  return (
    <div>
      <button onClick={moveForward}>move to forward</button>

      <div ref={elmRef}>translate this element</div>
    </div>
  );
}

go to List

Storage

Get or set data from localStorage or sessionStorage with Storage by buttom keywords:

  • getLocal
  • setLocal
  • getSession
  • setSession
import { Storage } from 'react-flux-context';

fuction api() {
  const prePath = Storage.getLocal('__auth__');
  console.log(prePath.role)

  Storage.setLocal('__user__', {
    first_name: 'jack',
    second_name: 'jaki',
    age: 12
  }})

}

go to List

License

MIT © mohammadbrzbrz72