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

v1.1.0

Published

Gives you setGlobalState, so you can set state globally

Downloads

1,399

Readme

react-globally

Gives you setGlobalState, so you can set state globally

travis build Coveralls branch version

What is this?

This lib gives you two things:

  • setGlobalState: A function with the exact same API of setState but that sets the state globally
  • globalState: The global state that's updated when you call setGlobalState

You receive both via props wrapping any component with the withGlobalState function. To use withGlobalState, you will have to wrap your app with a Provider that receives the initial state.

This way you can use setState to manage the local state of a Component and setGlobalState to manage the global state with the same API.

Key benefits

  • No need to learn a new API: If you know how to use setState, you know how to use setGlobalState
  • Simplicity: Just wrap your components with a function and that's it
  • Progressive: Start with setState, if there's the need, change it to setGlobalState

Installation

You can install it via npm:

npm install --save react-globally

CDN

If you prefer to exclude react-globally from your application and use it globally via window.ReactGlobally, the react-globally package provides single-file distributions via unpkg:

<!-- development version -->
<script src="https://unpkg.com/react-globally@latest/dist/umd/react-globally.js"></script>

<!-- production version -->
<script src="https://unpkg.com/react-globally@latest/dist/umd/react-globally.min.js"></script>

Usage

First you have to wrap your app with the Provider giving it the initial state:

// index.js
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-globally'
import CounterControls from './CounterControls'
import CounterInfo from './CounterInfo'

const initialState = {
  counter: 0
}

class App extends Component {
  render () {
    return (
      <div>
        <CounterControls />
        <CounterInfo />
      </div>
    )
  }
}

ReactDOM.render(
  <Provider globalState={initialState}>
    <App />
  </Provider>,
  document.getElementById('root')
)

Then you wrap the components that should have a global state with withGlobalState:

// CounterControls.js
import React, { Component } from 'react'
import { withGlobalState } from 'react-globally'

class CounterControls extends React.Component  {
  increment = () => {
    this.props.setGlobalState(prevGlobalState => ({
      counter: prevGlobalState.counter + 1
    }))
  }

  decrement = () => {
    this.props.setGlobalState(prevGlobalState => ({
      counter: prevGlobalState.counter - 1
    }))
  }

  zero = () => {
    this.props.setGlobalState({
      counter: 0
    })
  }

  render () {
    return (
      <div>
        <button onClick={this.increment}>Increment</button>
        <button onClick={this.decrement}>Decrement</button>
        <button onClick={this.zero}>Set to Zero</button>
      </div>
    )
  }
}

export default withGlobalState(CounterControls)

You can access and set the global state anywhere in the tree, and it works with stateless functional components too, since it's just props:

// CounterInfo.js
import React from 'react'
import { withGlobalState } from 'react-globally'

const CounterInfo = (props) => {
  return (
    <div>
      The counter value: {props.globalState.counter}
      <button onClick={() => props.setGlobalState({ counter: 100 })}>Set to 100</button>
    </div>
  )
}

export default withGlobalState(CounterInfo)

Reusing your code

You can extract your setState and/or setGlobalState calls to pure functions, reusing and testing them in isolation.

See this tweet from Dan Abramov.

Other solutions