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-water-tank

v1.0.4

Published

A simple library to manage global states in React/React Native projects

Downloads

3

Readme

React Water Tank

This is a simple library to manage the global states like Redux/Flux does. You can use it both in a React JS project or a React Native project! No more redux or context API or Flux! This is also controlled using event-driven architecture, and you can connect your components using a hoc or a hook which has the direct communication with the store. This is very simple and there is no complexity to use it like redux/flux or react context. You can register your states as a state holder easily and manage them from their own methods (You will be given from the hooks or from the hoc props!)

Documentation

Installation

npm install react-water-tank

Usage

Create Global States

Create a folder named states at the root of your project Inside the states directory you can put your global states like below

// AuthState.js
export const AuthState = {
    is_logged_in: false,
    user_id: '',
};

// TodoState.js
export const TodoState = {
    data: [],
};

Register State Holders (Global States)

Put these line of codes into a top level component such as index.js or App.js If you do not register the state holders into a top level component, that would not be a problem, but you might not be able to access the later defined states into some components that are mounted before the state holder registration.

import {registerStateHolder} from "react-water-tank";
import {AuthState, TodoState} from './states';

registerStateHolder("auth", AuthState);
registerStateHolder("todo", TodoState);

Unregister State Holders (Global States)

You can unregister a state holder object by the following way.

import { unregisterStateHolder } from "react-water-tank";

unregisterStateHolder("todo");

Dispatching

You can call the following function to dispatch store changes manually

import { dispatch } from "react-water-tank";

dispatch();

Connect to store from a component

Both for functional or class component you can use a HOC which will pass a prop as the state holder name. See the below code carefully:

Class Component

import { connectWaterTankPipe } from "react-water-tank";

class Todos extends React.Component {
    render() {
	    // Grab the state and it's change method
	    const {state, setState} = this.props.todo;
	    
	    return (
		    <React.Fragment>
			    {/* Your JSX */}
		    </React.Fragment>
	    );
    }
}

// Here you need to pass the state holder name
// For creating connection to the state directly
export default connectWaterTankPipe("todo")(Todos);

Functional Component

import { connectWaterTankPipe } from "react-water-tank";
import { Button } from "./components/Button";

function Login(props) {
    const {state, setState} = props.auth;
    
    const handleClick = () => setState({
	    is_logged_in: true,
    });
    
    return (
	    <React.Fragment>
		    <Button
			    onClick={handleClick}
			    title={"Login"}
		    />
	    </React.Fragment>
    );
}

// Here you need to pass the state holder name
// For creating connection to the state directly
export default connectWaterTankPipe("auth")(Login);

Hooks Support

You can also use hooks to make the easier connection with a state holder Follow the below code (The usage of hooks)

import { useWaterTankPipe } from "react-water-tank";
import { Button } from "./components/Button";

function Login(props) {
    const [state, setState] = useWaterTankPipe("auth");
    
    const handleClick = () => setState({
	    is_logged_in: true,
    });
    
    return (
	    <React.Fragment>
		    <Button
			    onClick={handleClick}
			    title={"Login"}
		    />
	    </React.Fragment>
    );
}

export default Login;

Update multiple state holder object

import { setStateHolderState } from "react-water-tank";
import { Button } from "./components/Button";

function Home(props) {
    
    const logout = () => setStateHolderState("auth", {
	    is_logged_in: false,
    });
    
    const clearTodos = () => setStateHolderState("todo", {
	    data: [],
    });
    
    return (
	    <React.Fragment>
		    <Button
			    onClick={logout}
			    title={"Logout"}
		    />
		    
		    <Button
			    onClick={clearTodos}
			    title={"Clear Todos"}
		    />
	    </React.Fragment>
    );
}

export default Home;

Developer

Mohammed Nayeem GitHub: http://github.com/chiefnayeem