1.0.4 • Published 5 years ago

reconstate v1.0.4

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

Reconstate

Eeasy state management and tracking of Provider/Subscribe/connect/dispatch relationships when using React's Context API

Demo

Check Demo

npm install reconstate

App.js

import { Provider } from 'reconstate';
const initState = {
    count: 1
}

const config = {
    persist: {
        storage: AsyncStorage // localStorage with web
    }
}

class App extends React.Component {
    render() {
        return (
            <Provider value={initState} config={config}>
                <YourAppComponent>
            </Provider>
        )
    }
}

Connect

import { connect } from 'reconstate'

class Counter extends React.PureComponent {
    render() {
        const { count, dispatch } = this.props
        return (
            <View style={{ flex: 1 }}>
                <Text>{count}</Text>
                <Button
                    onPress={() => dispatch(state => ({ count: state.count + 1 }))} title="+"
                />
            </View>
        )
    }
}

const mapStateToProps = (state, props) => ({
    count: state.count
})

export default connect(mapStateToProps)(Counter)

Subscribe

import { Subscribe } from 'reconstate'

class Subscriber extends React.PureComponent {
    render() {
        return (
            <View style={{ flex: 1 }}>
                <Subscribe>
                    {({ count }) => {
                        return <Text>{count}</Text>
                    }}
                </Subscribe>
            </View>
        )
    }
}

getContext

import { getContext } from 'reconstate'


getContext(state => {
    // return global state
})
getContext().then(state => {
    // return global state
})

Dispatch

import { dispatch } from 'reconstate'

const count = value => {
    dispatch((state) => ({ count: state.count + value }))
}
1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago