微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

unistore 特性Usage 微型集中式状态容器

程序名称:unistore 特性Usage

授权协议: MIT

操作系统: 跨平台

开发语言: JavaScript

unistore 特性Usage 介绍

unistore 是一个微型(650B)的集中式状态容器。

特性

  • 非常小却很好地遵循了 Preact 的步伐

  • 类似 Redux 的熟悉的命名和思路

  • 有用的数据选择器,可从状态中提取属性

  • 便携式的操作,可移动到一个通用的地方并导入

  • 简化是唯一目标

Usage

import { Provider, createStore, connect } from 'unistore'

let store = createStore({ count: 0 })

// If actions is a function, it gets passed the store:
let actions = store => ({
    // Actions can just return a state update:
    increment(state) {
        return { count: state.count+1 }
    },

    // The above example as an Arrow Function:
    increment2: ({ count }) => ({ count: count+1 }),

    // Async actions can be pure async/promise functions:
    async getStuff(state) {
        let res = await fetch('/foo.json')
        return { stuff: await res.json() }
    },

    // ... or just actions that call store.setState() later:
    incrementAsync(state) {
        setTimeout( () => {
            store.setState({ count: state.count+1 })
        }, 100)
    }
})

const App = connect('count', actions)(
    ({ count, increment }) => (
        <div>
            <p>Count: {count}</p>
            <button onClick={increment}>Increment</button>
        </div>
    )
)

export default () => (
    <Provider store={store}>
        <App />
    </Provider>
)

unistore 特性Usage 官网

https://github.com/developit/unistore

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐