create-object-context
TypeScript icon, indicating that this package has built-in type declarations

0.0.19 • Public • Published

A Simple implementation of react context for sharing an object between components without causing unnecessary rendering

Usage

// Initial value is Required!
const initialValue = {
    lang: 'en',
    color: 'dark' ,
    firstVisit: null
}

// "createObjectContext" - This is where the magic happens
const ExampleContext = createObjectContext(initialValue)

// You can now wrap your components as you normally would
const ParentComponent = ()=> {
    const lang = 'he'
    const color = 'light'
    const firstVisit = true
    return (
        <ExampleContext.Provider value={{ lang, color, firstVisit }}>
            <ChildComponent />
        </ExampleContext.Provider>
    )
}

const ChildComponent = ()=> {
    // Select only the properties you want to use
    const { color, lang } = ExampleContext.use('color', 'lang')
    return (
        <div>The color is {color}, and the language is {lang}</div>
    )
}

Readme

Keywords

none

Package Sidebar

Install

npm i create-object-context

Weekly Downloads

13

Version

0.0.19

License

MIT

Unpacked Size

8.27 kB

Total Files

7

Last publish

Collaborators

  • natqe3