项目作者: achaljain

项目描述 :
React state management using context
高级语言: JavaScript
项目地址: git://github.com/achaljain/smart-context.git
创建时间: 2020-12-24T16:30:53Z
项目社区:https://github.com/achaljain/smart-context

开源协议:MIT License

下载





Logo


smart-context



React state management made easy


https://smart-context.netlify.app



npm version Build Status Coverage Status semantic-release

Highlights

  • Lightweight
  • Zero setup. No boilerplate
  • 100% config driven
  • Async actions
  • Extend with plugins
  • Multiple stores/contexts
  • Easy Debugging
  • Typescript support

Installation

npm

  1. npm install smart-context

yarn

  1. yarn add smart-context

Quick start in 3 steps

Create store

You can create multiple stores. All stores must have a unique name.

  1. // store.js
  2. // Create initial state
  3. const initialState = { name: '' }
  4. // Create actions
  5. const actionsConfig = {
  6. setName: ['name'],
  7. }
  8. // Provide a good name
  9. const displayName = 'myContext'
  10. // Setup is done! Export config
  11. export default {
  12. initialState,
  13. actionsConfig,
  14. displayName,
  15. }

Plugin smart-context

  1. // Wrap root component in smart-context HOC
  2. import React from 'react'
  3. import { WithContextProvider } from 'smart-context'
  4. import Config from './store'
  5. const App = ({ children }) => <div id="app-container">{children}</div>
  6. export default WithContextProvider(App, [Config])

Access store

  1. // myAwesomeComponent.jsx
  2. import React from 'react'
  3. import { useSmartContext } from 'smart-context'
  4. const MyAwesomeComponent = () => {
  5. const {
  6. state: { name },
  7. actions: { setName, reset },
  8. } = useSmartContext('myContext')
  9. const clickHandler = () => {
  10. setName({ name: 'smart-context' })
  11. }
  12. const resetHandler = () => {
  13. reset()
  14. }
  15. return (
  16. <>
  17. <button onClick={clickHandler}>Say Hi</button>
  18. <button onClick={resetHandler}>Reset</button>
  19. {name ? <h1>Hi, {name}</h1> : null}
  20. </>
  21. )
  22. }
  23. export default MyAwesomeComponent

Documentation

Visit website for full documentation and demo.

Contributing

Refer Contributing Guide.

License

MIT licensed.