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

context-store

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

context-store

[![License MIT](https://img.shields.io/dub/l/vibe-d.svg)](LICENSE) [![NPM Version](https://img.shields.io/npm/v/context-store.svg)](https://www.npmjs.com/package/context-store)

0.2.2
npm
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

context-store

License MIT NPM Version

使用 React Context API 实现的简单全局状态管理

npm install context-store

示例

// Store.js

import contextStore from 'context-store'

export default contextStore({
  message: 'hello',
  updateMessage(message) {
    this.setState({ message })
  },
})
// AComponent.js
// 方式一:需要获取状态时,使用 <Store.Consumer> 包裹

import React from 'react'
import Store from './Store'

export default () => (
  <Store.Consumer>{context => <div>{context.message}</div>}</Store.Consumer>
)
// BComponent.js
// 方式二:创建带状态的组件 Store.Component()

import React from 'react'
import Store from './Store'

export default Store.Component((props, context) => (
  <button onClick={() => context.updateMessage(props.message)}>
    updateMessage
  </button>
))
// index.js

import React from 'react'
import { render } from 'react-dom'
import Store from './Store'
import AComponent from './AComponent'
import BComponent from './BComponent'

const App = () => (
  <Store.Provider>
    <AComponent />
    <BComponent message="world" />
  </Store.Provider>
)

render(<App />, document.getElementById('root'))

Keywords

react

FAQs

Package last updated on 02 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