https://github.com/timbroddin/react-global-state
🤗 Global state for your react-components.
https://github.com/timbroddin/react-global-state
Last synced: 8 months ago
JSON representation
🤗 Global state for your react-components.
- Host: GitHub
- URL: https://github.com/timbroddin/react-global-state
- Owner: TimBroddin
- Created: 2016-12-05T16:09:05.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-12-05T16:19:15.000Z (over 9 years ago)
- Last Synced: 2025-10-11T22:16:07.575Z (9 months ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 7
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# react-global-state
Sometimes you want global state in React, but you don't want the added complexity of using Redux. What if calling `setState` could update your entire state tree?
## Installation
`npm install --save react-global-state`
or
`yarn add react-global-state`
## Usage
Wrap your components with `gs`. Your global state will be available as the `state` and `setState` props.
import {gs} from 'react-global-state';
class Button extends Component {
onClick() {
const {state, setState} = this.props;
setState({counter: (state.counter) ? state.counter+1 : 1});
}
render() {
const {state} = this.props;
return
Click me
}
}
class Display extends Component {
render() {
const {state} = this.props;
return Counter value: {state.counter}
}
}
const WrappedButton = gs(Button);
const WrappedDisplay = gs(Display);
class App extends Component {
render() {
const {state} = this.props;
return (
);
}
}
## Inspired by...
Redux, which I love and which you should use if your app has the least bit of complexity.