New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

context.vue

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

context.vue

The Vue implementation of React context api.

  • 0.1.1
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

CONTEXT

React context API like Implementation in Vue.

Context provides a way to pass data through the component instance tree without having to pass props down manually at every level.

When to Use Context

You should always take into consideration of Store first.

It will be necessary when you find the dependency between components and Store make it difficult for you to decouple your project into components.

Getting Start

  1. Install vis npm, npm install --save context.vue
  2. Install to Vue as a plugin
import Context from 'context.vue'

Vue.use(Context)

Now the method this.$createContext is in your component, and components Provider and Consumer are available.

Usage

For example, in the code below we manually thread through a “theme” prop in order to style the Button component:

@Component({
  props: {
    theme: String,
  },
})
class ThemedButton extends Vue {
  render(h) {
    return <Button theme={this.theme}></Button>
  }
}

@Component({})
class Layout extends Vue {
  data() {
    return { theme: 'dark' }
  }

  render(h) {
    return <ThemedButton theme={this.theme}></ThemedButton>
  }
}

Using context, we can avoid passing props through intermediate elements:

@Component({
  template: `
    <consumer>
      <template slot-scope="{ theme }">
        <button :theme="theme"></button>
      </template>
    </consumer>`,
})
class ThemedButton extends Vue {
}

@Component({})
class Layout extends Vue {
  data() {
    return {
      state: this.$createContext({
        theme: 'dark',
      }),
    }
  }

  render(h) {
    return (
      <provider value={this.state}>
        <ThemedButton></ThemedButton>
      </provider>
    )
  }
}

Examples

Updating Context from a Nested Component

themed-button.vue

<template>
  <consumer>
    <template slot-scope="{ theme, dispatch }">
      <button :theme="theme" @click="dispatch('changeTheme', 'red')"></button>
    </template>
  </consumer>
</template>

layout.vue

<template>
  <provider :value="state">
    <themed-button></themed-button>
  </provider>
</template>
<script>
export default {
  data() {
    return {
      state: this.$createContext({
        theme: 'dark',
      }, {
        changeTheme(context, color) {
          context.theme = color
        },
      }),
    }
  },
}
</script>

Consuming Multiple Contexts

If you have to implement this, please use Vuex instead of Context API,

Keywords

FAQs

Package last updated on 31 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc