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

observable-hook

v0.5.0

Published

this is a react-hook which helps you to make stores without needing context and Provider component

Downloads

12

Readme

#observable-hook

this library will let you to make a simple object as store makeObservable function is the only function of this component which exports as default this function can be called by a function which will receive updateComponents function and will return the store object in this function you can set eventListeners and many other things but any function which changes properties of store must call updateComponents function manually to call every observer components to make them selves update

##motivation

using store as global without needing context

after new ContextAPI of react many users decided to not use redux any more because it was do nothing for them and useReducer have everyThings users need but having a global store have some advantages over context which are

  1. when you you have your store object you can use it in callbacks function out of react-components or inside them but without need to add it as callback dependency (because it will never changes)
  2. and when you don't want to listen on store changes (don't want to rerender your component) you can simply use global store

// MyComponent.jsx 

export default function MyComponent(props) {
    useEffect(() => {
        console.log(store);
    }, []);
    const foo = useCallback(() => {
        console.log(store);
    }, []);
    return (
        // ...
    );
}


// somewhere out of react components 
// tasks.js

function myTask(data) {
    // do some logic here
    store.setData(data);
}

as you can see in these two examples we didn't need store data for MyComponent rendered value so when it's data changed we don't want our component to rerender and also we had use store data in useEffect and useCallback functions without needs to use useReducer (which will cause rerendering after store data updated)

  1. one other advantage of this library is you don't need context provider or consumer any more so when you want to use a data inside of an external non-react library which it will handle dom with itself you would have a problem using consumer because these libraries are not inside of your react hierarchy and tey can not use data you had provide in root of your tree (examples of these libraries which may handle dom by them selves are modals confirms toasts charts and ...) in these cases steel can use a global store becouse they don't need a provider at root of tree
  2. with using this library you don't need to write in root of your app (I think that writing that is wasting time)

##API

declare function createObservable<T extends object>(storeFactor: (updateComponents: VoidFunction) => T): readonly [T, () => T];

as you can see of declaration of our function it will get only one function named storeFactor storeFactor it self is a function that will receive componentsUpdater and will return our store componentsUpdater can be used in storeFactor body itself or can be used in store action methods which changes store properties

##Example in the following we can see a ViewPortStore which tells us about screen size


// view-store.js
const [viewPort, useViewPort] = createObservable(update => {
    const viewPort = {
        width: window.innerWidth,
        height: window.innerHeight,
        get isMobile() {
            return this.width <= MOBILE_SCREEN_WIDTH;
        }
    };

    window.addEventListener('resize', () => {
        viewPort.width = window.innerWidth;
        viewPort.height = window.innerHeight;
        update()
    });

    return viewPort;
});
export {viewPort, useViewPort};
//

// MyComponent.jsx 
import {viewPort, useViewPort} from 'view-store'
export default function ComponentWithOutObserve(props) {
    const foo = useCallback(() => {
        console.log(store);
    }, []);
    return (
        // ...
    );
}

export default function ComponentWithObserve(props) {
    const viewPort = useViewPort();
    useEffect({
        console.log('width changed');
    }, [viewPort.width]);

    return (
        // ...
    );
}