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

rosma

v1.6.0

Published

Simple and easy-to-use global state management for React.

Downloads

480

Readme

Rosma

Simple and easy-to-use global state management for React. no need for provider, reducer or nothing else!

Installation

npm i rosma

Documentation

https://rosma.dev/

Counter app

import { useObserver } from 'rosma';

export default function Counter() {
  const { count, setCount } = useObserver(0);

  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}

No order for variables

import { useObserver } from 'rosma';

export default function Counter() {
  const { setCount, count } = useObserver(0);

  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}

The only concept that must be notice is that you must add a 'set' to the beginning of the setter name of the destructed variable.

For example if you want a setter method for the 'count' variable, you should name that setter as 'setCount'.

Also, all the destructed variables are case insensitive

import { useObserver } from 'rosma';

export default function Counter() {
  const { CouNt, setcOunT } = useObserver(0);

  return <button onClick={() => setcOunT(Count + 1)}>{CouNt}</button>;
}

Multiple destructuring

Destruct all the variables you want at once

import { useObserver } from 'rosma';

export default function Counts() {
  const { count, setCount, count1, setCount1 } = useObserver(0);

  return (
    <>
      <button onClick={() => setCount(count + 1)}>{count}</button>
      <button onClick={() => setCount1(count1 + 1)}>{count1}</button>
    </>
  );
}

Different initial values

In the example above, all the variables take the initial value of 0. to avoid this, one of the following methods can be used:

  • Define the initial values separately
import { useObserver } from 'rosma';

export default function Counts() {
  const { count, setCount } = useObserver(10);
  const { count1, setCount1 } = useObserver(20);

  return (
    <>
      <button onClick={() => setCount(count + 1)}>{count}</button>
      <button onClick={() => setCount1(count1 + 1)}>{count1}</button>
    </>
  );
}
  • Define the initial values at once

If you want to set the initial values and also destruct them at once, you can set the initial values directly from the observer.

import { observer, useObserver } from 'rosma';

observer.set({ count: 10, count1: 20 });

export default function Counts() {
  const { count, setCount, count1, setCount1 } = useObserver();

  return (
    <>
      <button onClick={() => setCount(count + 1)}>{count}</button>
      <button onClick={() => setCount1(count1 + 1)}>{count1}</button>
    </>
  );
}

Notes app

import { useObserver } from 'rosma';

export default function Note() {
  const { note = '', setNote, notes = [], setNotes } = useObserver();

  return (
    <>
      <input value={note} onChange={(e) => setNote(e.target.value)} placeholder="write something" />
      <button
        onClick={() => {
          setNotes(notes.concat(note));
          setNote('');
        }}
      >
        Add
      </button>
      <ul>
        {notes.map((note, index) => (
          <li key={index}>{note}</li>
        ))}
      </ul>
    </>
  );
}

Avoiding extra rerender

If you want the entire main component not to be re-rendered with state changes, you can easily split the elements of the main component into smaller components using the withState method.

import { withState } from 'rosma';

const Input = withState(({ note = '', setNote }) => <input value={note} onChange={(e) => setNote(e.target.value)} placeholder="write something" />);

const Button = withState(({ note, setNotes, setNote }) => (
  <button
    onClick={() => {
      setNotes((notes = []) => notes.concat(note));
      setNote('');
    }}
  >
    Add
  </button>
));

const List = withState(({ notes = [] }) => (
  <ul>
    {notes.map((note, index) => (
      <li key={index}>{note}</li>
    ))}
  </ul>
));

export default function Note() {
  return (
    <>
      <Input />
      <Button />
      <List />
    </>
  );
}

Further optimization

To avoid extra rendering, you can read the values directly from the observer. In the example above, the Button component is re-rendered every time that the 'note' variable is changed. But you can avoid this extra rendering by reading the 'note' value from the observer.

import { observer, withState } from 'rosma';

const Input = withState(({ note = '', setNote }) => <input value={note} onChange={(e) => setNote(e.target.value)} placeholder="write something" />);

const Button = withState(({ setNotes, setNote }) => (
  <button
    onClick={() => {
      setNotes((notes = []) => notes.concat(observer.get('note')));
      setNote('');
    }}
  >
    Add
  </button>
));

const List = withState(({ notes = [] }) => (
  <ul>
    {notes.map((note, index) => (
      <li key={index}>{note}</li>
    ))}
  </ul>
));

export default function Note() {
  return (
    <>
      <Input />
      <Button />
      <List />
    </>
  );
}