react-pendulum
v0.0.1
Published
Use the power of the Pendulum and change the course of the duel! react-pendulum a React Context utility libraries
Downloads
14
Maintainers
Readme
react-pendulum
Use the power of the Pendulum and change the course of the duel!
react-pendulum a React Context utility libraries
Install
Using npm
npm install --save react-pendulum
Using yarn
yarn add react-pendulum
Components
MultiProvider
A component to nicely and readably wrap components with multiple providers
Props
providers
the array of providers instances to wrap to thechildren
import React, { Component, createContext } from 'react'
import { MultiProvider } from '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 pass the context value as props to a component.
Args:
Context
the context to assign the valuepropsName
the props name to assign the value ofContext
import React, { Component, createContext } from 'react'
import { withContext } from 'pendulum'
const NameContext = createContext<string>('John Doe')
const withName = withContext(Context1, 'name');
const HelloWorld = withName(({ name }) => {
return <>{`Hello ${name}`}</>
})