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-observable-context

v1.0.4

Published

using observable context gives the decision whether to rerender or not to the children using the context

Downloads

7

Readme

Introduction

Using observable context gives the decision whether to rerender or not to the children using the observable context

Install

yarn add react-observable-context
# or
npm i react-observable-context --save

Usage / Example - Observable Context Provider:

in this example we create a context to hold population numbers of a city. It provides a method 'setPopulation' to change the population number for a city. Once the population is being changed, all consumer are being notified about the changed populations. It's up to the consumer to rerender or not


export const ObservedPopulationsContext = React.createContext(null);

const PopulationsContext = React.createContext(null);

export function usePopulationContext() {
    const context = useContext(PopulationsContext)
    if (context === undefined) {
        throw new Error('Context must be used within a provider')
    }
    return context
}

const initialValues = [
    ["Paris", 2],
    ["Toronto", 2],
    ["Hamburg", 1],
    ["New York", 8]
];

export function ObservablePopulationsProvider({children}){

    const [populations, setPopulations] = useState(new Map(initialValues))
    const {notify, subscribe} = useObservable(notifyListener)

    /**
     * notify all subscribers once populations has been changed
     */
    useEffect(() => {
        notifyListener()
    }, [populations])

    /** notify all dependend components on relevant data changes
     *  @param index if null or undefined, all components get notified
     *  otherwise online the component with the given index will be notified
     **/
    function notifyListener(index){
        notify(populations, index)
    }

    /**
     * custom method to mutate data. 
     * This method is separately returned by usePopulationContext.
     * normal useContext(Population
     * @param id city name
     * @param population population number in million
     */
    const setPopulation = function(id, population){
        const lPopulations = new Map([...populations])

        lPopulations?.set(id, population)
        setPopulations(lPopulations)
    }

    return <ObservedContext.Provider value={subscribe}>
        <PopulationsContext.Provider
            value={{setPopulation}}>
            {children}
        </PopulationsContext.Provider>
    </ObservedContext.Provider>
}

Usage / Example - Observable Context Consumer:

this example show the usage of using observable contexts. Imagine we have multiple components that display the population number beneath the city name and the number of rerenders that has been occured. It also provides the ability to increase the population of the respective city.

Technically the component takes the city name as argument to fetch the population number from the population context. Without an observable context all components using the population context would rerender even when their population number hasn't been changed.

    export default function CityPopulation(props){

    const [rerenders, setRerenders] = useState(0)
    const [myPopulation, setMyPopulation] = useState()

    /**
     * if myPopulation has been changed, increase the number of rerenders
     */
    useEffect(() => {
        if (!myPopulation) return
        setRerenders(rerenders + 1)
    }, [myPopulation])

    /**
     * using an observable context for populations in order to update myPopulation only if it has been changed.
     * it will not rerender if the populations that are being provided by the observable context either has no entry for the city name
     * or the respective population number hasn't been changed.
     */
    useObservedContext((populations) => {
        console.log('received populations: ', populations)
        if (!populations?.has(props.name)) return
        if (populations.get(props.name) === myPopulation) return
        setMyPopulation(populations.get(props.name))
    }, ObservedPopulationsContext)

    /**
     * the population context also provides a function to increase the population for a city by
     * passing the city name and the population number
     */
    const {setPopulation} = usePopulationContext()

    /**
     * this increases the population of this components city
     */
    const increasePopulation = function(){
        setPopulation(props.name, myPopulation + 1)
    }

    return <Card key={props.name} style={{ width: '18rem', color: "#000" }}>
        <Card.Body>
            <Card.Title>{props.name}</Card.Title>
            <Card.Text>
                <div>Population: {myPopulation}</div>
                <div>Rerenders: {rerenders}</div>
            </Card.Text>
            <Button onClick={increasePopulation} variant="primary">Increase</Button>
        </Card.Body>
    </Card>
}