Skip to content

alfredosalzillo/react-pendulum

Repository files navigation

react-pendulum

A React Context utility library.

NPM JavaScript Style Guide codecov

Install

Using npm

npm install --save react-pendulum

Using yarn

yarn add react-pendulum

Components

MultiProvider

A component to clean up the nasty code you get from taking advantage of React Context API.

Props

  • providers the array of providers instances to wrap to the children
import React, { Component, createContext } from 'react'
import { MultiProvider } from 'react-pendulum'

const FirstNameContext = createContext<string>('John')
const LastNameContext = createContext<string>('Doe')

const HelloWorld = () => {
  const firstName = useContext(FirstNameContext)
  const lastName = useContext(LastNameContext)
  return <>{`Hello ${firstName} ${lastName}`}</>
}

class App extends Component {
  render() {
    return (
      <MultiProvider
        providers={[
          <FirstNameContext.Provider value='Yugi' />,
          <LastNameContext.Provider value='Muto' />
        ]}
      >
      <HelloWorld />
    </MultiProvider>
    )
  }
}

Utilities

withContext

A high order function to create the withContext HOC for any Context.

Args:

  • Context the context to assign the value
  • propsName the props name to assign the value of Context
import React, { Component, createContext } from 'react'
import { withContext } from 'react-pendulum'

const NameContext = createContext<string>('John Doe')

const withName = withContext(NameContext, 'name');
const HelloWorld = withName(({ name }) => {
  return <>{`Hello ${name}`}</>
})

License

MIT ©

Releases

No releases published

Packages

No packages published