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-fusion-state

v0.0.61

Published

![Alt text](images/react-fusion-state.png)

Downloads

8

Readme

Alt text

ReactFusionState is a global state management library for React and React Native applications, designed to be simple and efficient. It allows you to create and manage global states without the need for atoms, similar to RecoilState but more streamlined.

Installation

To install ReactFusionState, use npm or yarn:

npm install react-fusion-state

or

yarn add react-fusion-state

Usage

1. FusionStateProvider

The FusionStateProvider component is used to provide the global state context to your React application.

Props:

  • children: ReactNode - The child components that will have access to the global state.

Example:

import React from "react";
import { FusionStateProvider } from "react-fusion-state";
import App from "./App";

const Root = () => (
  <FusionStateProvider>
    <App />
  </FusionStateProvider>
);

export default Root;

2. useFusionState

The useFusionState hook is used to create and manage a global state.

Prototype:

const useFusionState = <T,>(key: string, initialValue?: T): [T, React.Dispatch<React.SetStateAction<T>>];

Parameters:

  • key: string - The unique key for the state.
  • initialValue?: T - The initial value for the state. If not provided and the key does not exist, an error will be thrown.

Returns:

  • [T, React.Dispatch<React.SetStateAction>] - An array containing the state and a function to update the state.

Example:

import React from 'react';
import { useFusionState } from 'react-fusion-state';

const CounterComponent: React.FC = () => {
  const [count, setCount] = useFusionState<number>('count', 0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={() => setCount(count - 1)}>Decrement</button>
    </div>
  );
};

export default CounterComponent;

3. useFusionStateLog

The useFusionStateLog hook is used to retrieve the global state.

Prototype:

const useFusionStateLog = (keys?: string[]): any;

Parameters:

  • keys?: string[] - An optional array of keys to retrieve specific states. If not provided, all states will be returned.

Returns:

  • any - The global state or the specified states.

Example:

import React from 'react';
import { useFusionStateLog } from 'react-fusion-state';

const StateViewer: React.FC = () => {
  const allState = useFusionStateLog();
  const specificState = useFusionStateLog(['count', 'nonExistentKey']);

  return (
    <div>
      <div>
        <h3>All State</h3>
        <pre>{JSON.stringify(allState, null, 2)}</pre>
      </div>
      <div>
        <h3>Specific State</h3>
        <pre>{JSON.stringify(specificState, null, 2)}</pre>
      </div>
    </div>
  );
};

export default StateViewer;

Complete Example

Here is a complete example of how to use ReactFusionState in a React application, including accessing the global state in a different component.

App.tsx:

import React from 'react';
import { useFusionState, useFusionStateLog } from 'react-fusion-state';

const CounterComponent: React.FC = () => {
  const [count, setCount] = useFusionState<number>('count', 0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={() => setCount(count - 1)}>Decrement</button>
    </div>
  );
};

const AnotherComponent: React.FC = () => {
  const [count, setCount] = useFusionState<number>('count');

  return (
    <div>
      <p>Count in AnotherComponent: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={() => setCount(count - 1)}>Decrement</button>
    </div>
  );
};

const StateViewer: React.FC = () => {
  const allState = useFusionStateLog();
  const specificState = useFusionStateLog(['count', 'count2', 'nonExistentKey']);

  return (
    <div>
      <div>
        <h3>All State</h3>
        <pre>{JSON.stringify(allState, null, 2)}</pre>
      </div>
      <div>
        <h3>Specific State</h3>
        <pre>{JSON.stringify(specificState, null, 2)}</pre>
      </div>
    </div>
  );
};

const App: React.FC = () => (
  <div>
    <CounterComponent />
    <AnotherComponent />
    <StateViewer />
  </div>
);

export default App;

index.tsx:

import React from 'react';
import ReactDOM from 'react-dom';
import { FusionStateProvider } from 'react-fusion-state';
import App from './App';

ReactDOM.render(
  <FusionStateProvider>
    <App />
  </FusionStateProvider>,
  document.getElementById('root')
);

Bugs

If you find a bug, please post it as a new issue on the GitHub repository.

Contribute

Would like to contribute to the project ? Fork the ReactFusionState project, include your changes, and submit a pull request back to the main repository.

License

This project is licensed under the MIT License.

Author

This project is maintained by Jacques GERARD