All Projects → schiehll → react-globally

schiehll / react-globally

Licence: other
Gives you setGlobalState, so you can set state globally

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-globally

Getx pattern
Design pattern designed to standardize your projects with GetX on Flutter.
Stars: ✭ 225 (+922.73%)
Mutual labels:  state-management, state
CSGSI
A simple C# library to interface with Counter-Strike: Global Offensive's Game State Integration
Stars: ✭ 124 (+463.64%)
Mutual labels:  state, global
Freactal
Clean and robust state management for React and React-like libs.
Stars: ✭ 1,676 (+7518.18%)
Mutual labels:  state-management, state
Stateshot
💾 Non-aggressive history state management with structure sharing.
Stars: ✭ 128 (+481.82%)
Mutual labels:  state-management, state
Hydrated bloc
An extension to the bloc state management library which automatically persists and restores bloc states.
Stars: ✭ 181 (+722.73%)
Mutual labels:  state-management, state
Swiftdux
Predictable state management for SwiftUI applications.
Stars: ✭ 130 (+490.91%)
Mutual labels:  state-management, state
Xstate
State machines and statecharts for the modern web.
Stars: ✭ 18,300 (+83081.82%)
Mutual labels:  state-management, state
Radon
Object oriented state management solution for front-end development.
Stars: ✭ 80 (+263.64%)
Mutual labels:  state-management, state
Final Form
🏁 Framework agnostic, high performance, subscription-based form state management
Stars: ✭ 2,787 (+12568.18%)
Mutual labels:  state-management, state
Mobx Rest
REST conventions for Mobx
Stars: ✭ 164 (+645.45%)
Mutual labels:  state-management, state
Vue State Store
📮 VSS (Vue State Store) - Vue State Management (with Publish & Subscribe pattern)
Stars: ✭ 128 (+481.82%)
Mutual labels:  state-management, state
React Easy State
Simple React state management. Made with ❤️ and ES6 Proxies.
Stars: ✭ 2,459 (+11077.27%)
Mutual labels:  state-management, state
React Workshop
⚒ 🚧 This is a workshop for learning how to build React Applications
Stars: ✭ 114 (+418.18%)
Mutual labels:  state-management, state
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (+504.55%)
Mutual labels:  state-management, state
Reworm
🍫 the simplest way to manage state
Stars: ✭ 1,467 (+6568.18%)
Mutual labels:  state-management, state
Contextism
😍 Use React Context better.
Stars: ✭ 141 (+540.91%)
Mutual labels:  state-management, state
Yewdux
Redux-like state containers for Yew apps
Stars: ✭ 58 (+163.64%)
Mutual labels:  state-management, state
React Composition Api
🎨 Simple React state management. Made with @vue/reactivity and ❤️.
Stars: ✭ 67 (+204.55%)
Mutual labels:  state-management, state
Redux Rs
📦 A Rust implementation of Redux.
Stars: ✭ 158 (+618.18%)
Mutual labels:  state-management, state
Statefulviewcontroller
Placeholder views based on content, loading, error or empty states
Stars: ✭ 2,139 (+9622.73%)
Mutual labels:  state-management, state

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.

Why?

Read about it here.

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

Edit 6l06nklwmn

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].