🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

react-context-simply

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-context-simply

leveraging the react context api for state management with no boilerplate code

1.0.3
npm
Version published
Maintainers
1
Created
Source

React Context Simply

A small package that will helps you manage your app state using react context api and hooks

Installation

npm install react-context-simply --save

Minimal Example

Before start please note :

  • state is an immutable object, can not be changed directly only by dispatching action

constanst.js

// file holds all our constants will be used in dispatching actions also in reducer
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';

actions.js

import { INCREMENT, DECREMENT } from "./constants";
export const increment = () => ({ type: INCREMENT });
export const decrement = () => ({ type: DECREMENT });

reducers.js

import { INCREMENT, DECREMENT } from "./constants";
export default function countReducer(state, action) {
    switch (action.type){
      case INCREMENT:
        return state + 1;
      case DECREMENT:
        return state - 1;
      default:
        return state;
    }
}

store.js

import createStateContext from 'react-context-simply';
import countReducer from './reducers';
import * as actions from './actions'

const {
    useStateValue,
    StateProvider,
    StateContext
} = createStateContext({}, countReducer, actions);

const useCountState = useStateValue;
const CountState = StateProvider;
const CountStateContext = StateContext

export {
    useCountState,
    CountState,
    CountStateContext
};

app.js

import React from 'react';
import { CountState, useCountState } from './store.js';

function Counter() {
    const [state, actions] = useCountState()
    const {increment, decrement} = actions
    return (
      <View style={{flex: 1, flexDirection: 'column', alignItems: 'center', justifyContent: 'center'}}>
          <TouchableOpacity onPress={()=>{
            increment()
          }}>
            <Text>Increment</Text>
          </TouchableOpacity>
          <TouchableOpacity onPress={()=>{
            decrement()
          }}>
            <Text>Decrement</Text>
          </TouchableOpacity>
          <Text>{state}</Text>
      </View>
    );
}

export default class App extends React.Component {
  render() {
    return (
        <CountState>
          <Counter />
        </CountState>
    );
  }
}

for me i prefer to seperate the code into files but you can do it in one file, its all up to you

Limitation :

the consumer component it should be a functional component but its possible to acces state in class based component like this:

```
import React, { Component } from 'react';
import { CountStateContext } from './store';

class Counter extends Component {
    static contextType = CountStateContext;  
    render() {
        const [state, actions] = this.context;
    }
}

```

Keywords

react context

FAQs

Package last updated on 10 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts