GithubHelp home page GithubHelp logo

koolamusic / use-realm Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 258 KB

Manage state in your react app using realms that can be shared within components

Home Page: https://www.npmjs.com/package/use-realm

License: MIT License

TypeScript 100.00%
react state hooks

use-realm's Introduction

use-realm

Manage state in your react app using realms that can be shared within components, this is a small utilily library that enables you share state within your apps without the heavy lifting of reducers, context and other complex state management libraries, use-realm is WIP and best used within small projects.


Install

Install with yarn yarn add use-realm Install with npm npm install use-realm

How to use

Use realm exposes a simple API, where you can utilize useRealm and createRealm to share state values across your components.

  • useRealm, manages the values within the realm that needs to be tracked.
  • createRealm, is an utility method that exposes a react hook which handles reading the values within a realm and dispatching actions within that realm. There are no restrictions to how many components that are allowed to dispatch actions within a realm at a given point in time, however it's advisable to only dispatch from a single source and share values across multiple components.

Example with Javascript/JSX

/* constants.js
This can be any file within your components directory */
export const COUNTER = createRealm(0);
import React from 'react';
import { createRealm, useRealm } from 'use-realm';
import { COUNTER } from './constants';

const App = () => {
  const [state, setState] = useRealm(COUNTER);
  return (
    <React.Fragment>
      <h1>Use Realm Example</h1>
      <h3>{state}</h3>
      <section>
          <button onClick={() => setState(state + 1)}>+</button>
          <button onClick={() => setState(state - 1)} disabled={state === 0}>-</button>
      </section>
    </React.Fragment>
  );
}

Example with Typescript/TSX

/* constants.js
This can be any file within your components directory */
export const COUNTER = createRealm<number>(0);
const App = (): JSX.Element => {
    const [state, setState] = useRealm<number>(COUNTER);
    return (
        <React.Fragment>
            <h1>Use Realm Example</h1>
            <h3>{state}</h3>
            <section>
                <button onClick={() => setState(state + 1)}>+</button>
                <button onClick={() => setState(state - 1)} disabled={state === 0}>-</button>
            </section>
        </React.Fragment>
    );
}

Todo

  • Use Many Realms to compose multiple realms (cookbook)
  • Documentation Site to document props, cookbooks and examples

use-realm's People

Contributors

koolamusic avatar

Watchers

James Cloos avatar  avatar

use-realm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.