DEV Community

Cover image for React Context Simplifier
Harsh
Harsh

Posted on

React Context Simplifier

To simplify the react context api usage and reduce unnecessary renders I have created context-simplifier library. Adding usage details and example below. Feedback, feature requests, bugs can be raised here https://github.com/Harshdand/context-simplifier

Install

npm install --save context-simplifier
Enter fullscreen mode Exit fullscreen mode
yarn add context-simplifier
Enter fullscreen mode Exit fullscreen mode

Usage

To create new context

Use createContextProvider to create new context. It will return the provider for the created context

import React, { Component } from 'react'
import { createContextProvider } from 'context-simplifier'

const App = () => {
  const CountProvider = createContextProvider('count', 0)

  return (
    <>
      <CountProvider>
        <Counter />
        <Layout>
          <CountDisplay />
        </Layout>
      </CountProvider>
    </>
  )
}
Enter fullscreen mode Exit fullscreen mode

To use the context value use getContextValue

import React from 'react'
import { getContextValue } from 'context-simplifier'

const CountDisplay = () => {
  const count = getContextValue('count')

  return <div>{count}</div>
}

export default CountDisplay
Enter fullscreen mode Exit fullscreen mode

To get the setter for updating the context value

Use getContextAction to get the setter function which can be used to update the context value

import React from 'react'
import { getContextAction } from 'context-simplifier'

const Counter = () => {
  const setCount = getContextAction('count')

  const increment = () => {
    setCount((oldValue) => oldValue + 1)
  }

  const decrement = () => {
    setCount((oldValue) => oldValue - 1)
  }

  return (
    <>
      <button onClick={increment}>Increment</button>
      <button onClick={decrement}>Decrement</button>
    </>
  )
}

export default Counter
Enter fullscreen mode Exit fullscreen mode

API

createContextProvider(contextName, initialValue, reducerFunction)provider

Creates a context and returns the provider.

Param Type Description
contextName string:required Context name must be string without any spaces or special characters. Examples: count, countContext
initialValue any:optional Initial Value for the context
reducerFunction function:optional Reducer function to update context value. Reducer function will receive two params state and action

getContextValue(contextName)contextValue

Creates a context and returns the provider.

Param Type Description
contextName string:required Provide the context name to fetch its value

getContextAction(contextName)contextSetterFunction

Creates a context and returns the provider.

Param Type Description
contextName string:required Provide the context name to get its setter function

License

MIT © Harshdand

https://www.npmjs.com/package/context-simplifier

Top comments (3)

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Wouldn't those "getXYZ" functions be better off named "useXYZ" as they are internally hooks and that's what devs and linting etc look for when working out where to put things before early returns?

Collapse
 
harshdand profile image
Harsh

Ya should work. Thanks for the feedback.

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Looks great, I like the mutation function stuff