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

v1.1.0

Published

A lightweight library to call async functions in React Context API

Downloads

17

Readme

react-context-saga

Is a lightweight API to use easily async functions that modify the React Context State. Based in redux-saga.

Motivation

There is a new React Context API that seems to be the evolution of redux. As a redux developer I want to try it but using my old way redux-saga functions, since I never use all the power of the generator functions, I change it to use async functions.

Usage

  1. Create reducer using the saga async functions

  2. Create the provider using sagaName, the recently created reducer, and the initial values The state of this provider could be changed using dispatch method {type: "saga_async_function", payload: {color: "blue"}}

  3. There are 2 reserved saga async functions, set and merge set: Change certain value i.e. {type: "set", key:"color", payload: "red"} merge: Merge Immutable map i.e. {type: "merge", payload: { color: "red"}}

Example

As you can see in React Context Saga + Websockets, the structure is very simple, instead of create a context folder you can declare your contexts in the App.js directly and import the saga functions and the initial values from somewhere else.

import React from 'react';
import {createSagaProvider, createSagaReducer} from 'react-context-saga'
import {getConsumer} from 'react-context-saga'
import {useSagaState} from 'react-context-saga'
import Immutable from 'immutable';
import request from 'superagent';

const EXAMPLE_SAGA = {
    // In the saga functions you can do a lot of fun stuff
    // Call an API and save the response in the state
    getReactProyects: async (state, payload, dispatch)=>{
        return request.get("https://api.github.com/search/repositories?q=react&sort=stars&order=desc")
            .then((response)=>{
                return {
                    "reactProyects": response.body.total_count
                }
            })
    }
}
const exampleInitialValues =Immutable.fromJS({
  "reactProyects": 'n/a'
})
const exampleReducer = createSagaReducer(EXAMPLE_SAGA);
const ExampleProvider = createSagaProvider({sagaName:'example', reducer:exampleReducer, initialValues:exampleInitialValues })


function ExampleComponent() {
    // Get state and dispatch method, altought we will use only dispatch here
    const [state, dispatch] = useSagaState('example');
    return (                
        <div>
            <div>
                <button onClick={() => dispatch({type: 'getReactProyects'})}>Get React Proyects</button>
            </div>
            <div>
                React Proyects {state.get("reactProyects")}                
            </div>
        </div>
    )
}

// For non functional components
/*
const ExampleConsumer = getConsumer('example')
class ExampleComponent extends React.Component{
    render(){
        return ( 
            <ExampleConsumer>
                {([state, dispatch]) =>                
                    <div>
                        <div>
                            <button onClick={() => dispatch({type: 'getReactProyects'})}>Get React Proyects</button>
                        </div>
                        <div>
                            React Proyects {state.get("reactProyects")}                
                        </div>
                    </div>
                }
            </ExampleConsumer>
        )
    }
}
*/
function App(){  
  return (      
    <div className="App">      
      <section>     
          <ExampleProvider>
            <ExampleComponent/>
          </ExampleProvider>
      </section>
    </div>
  );
}

export default App;
/*