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-floorlamp

v0.0.10

Published

Straightforward state management for React

Downloads

216

Readme

react-floorlamp

install and test

Straightforward state management for React. Released under the MIT License.

Installation

To install the latest stable version of react-floorlamp:

npm install react-floorlamp

React State Management in 3 Easy Steps

Manage state efficiently across your React components in three easy steps.

Step 1: Create Your Instance of FloorLamp

First, create an instance of FloorLamp in a module.

// in a file: floor-lamp.js
import { FloorLamp } from "react-floorlamp";

export const floorLamp = new FloorLamp();

Step 2: Bind a React.Component Instance

Next, bind a React component instance in its lifecycle methods. You can do this in the componentDidMount method, where you register the component, and in the componentWillUnmount method to ensure proper cleanup.

import React from "react";
import { floorLamp } from "./floor-lamp.js";

class CaptionDisplay extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      caption: "Default Caption",
    };
  }

  componentDidMount() {
    // Bind the component instance
    floorLamp.addComponent("CaptionDisplay", this);

    // Optionally manage a collection of instances using props
    floorLamp.addComponent(`CaptionDisplay${this.props.id}`, this);
  }

  componentWillUnmount() {
    // Unbind the component instance on unmount
    floorLamp.removeComponent("CaptionDisplay");
    floorLamp.removeComponent(`CaptionDisplay${this.props.id}`);
  }

  render() {
    return (
      <div>
        <h1>Caption Display</h1>
        <p>{this.state.caption}</p>
      </div>
    );
  }
}

Step 3: Update the State from Anywhere

You can update the state from anywhere in your application by calling setState using the FloorLamp instance.

import { floorLamp } from "./floor-lamp.js";

// passing an object
floorLamp.setState("CaptionDisplay", { caption: "hello world" }, () => {
  // optional callback
});

// passing an updater function
floorLamp.setState("CaptionDisplay", prevState => {
  let count = prevState.count || 0;
  count++;

  return {
    caption: "hello world",
    count: count
  };
});

Binding a Mock Component Using a State Hook

You can also bind a mock component using a state hook for functional components.

import { useState, useEffect } from "react";
import { floorLamp } from "./floor-lamp.js";

function CaptionDisplay(props) {
  const [caption, setCaption] = useState("Default Caption");

  const mockComponent = {
    setState: (state) => {
      if (typeof state === 'function') {
        // state might be an updater function :)
      }
      if (state.caption) {
        setCaption(state.caption);
      }
    },
  };

  useEffect(() => {
    floorLamp.addComponent("CaptionDisplay", mockComponent);
    return () => {
      floorLamp.removeComponent("CaptionDisplay");
    };
  }, []);

  return (
    <div>
      <h1>Caption Display</h1>
      <p>{caption}</p>
    </div>
  );
}

License

react-floorlamp is released under the MIT License.

100% Free: react-floorlamp can be used freely in both proprietary and open-source projects.

Attribution is required: You must retain the author's name and the license information in any distributed code. These items do not need to be user-facing and can remain within the codebase.