react-scope-provider

1.0.0-1 • Public • Published

react-scope-provider

npm i react-scope-provider
// App.js
import React from 'react'
import { ScopeProvider } from 'react-scope-provider'
import View from './View'
import scope from './scope'

export default () => (
  <ScopeProvider scope={scope}>
    <View />
  </ScopeProvider>
)
// View.js
import React from 'react'
import { ScopeConsumer } from 'react-scope-provider'

export default props => (
  <ScopeConsumer>
    {({
      // scope can be any value, even React components
      Box,
      Heading,
    }) => (
      <Box>
        <Heading>View</Heading>
      </Box>
    )}
  </ScopeConsumer>
)

A default scope can be provided to the consumer when it's used outside of a provider.

// View.js with default scope
import React from 'react'
import { ScopeConsumer } from 'react-scope-provider'

export default props => (
  <ScopeConsumer defaultScope={scope}>
    {({
      Box,
      Heading,
    }) => (
      <Box>
        <Heading>View</Heading>
      </Box>
    )}
  </ScopeConsumer>
)

HOC

import React from 'react'
import { withScope } from 'react-scope-provider'

export default withScope(({
  scope: {
    Box,
    Heading
  }
}) => (
  <Box>
    <Heading>Hello</Heading>
  </Box>
))

Readme

Keywords

none

Package Sidebar

Install

npm i react-scope-provider

Weekly Downloads

75

Version

1.0.0-1

License

MIT

Unpacked Size

4.57 kB

Total Files

4

Last publish

Collaborators

  • jxnblk