Giter Club home page Giter Club logo

smox's Introduction

smox logo

NPM version NPM downloads

Smox

Fast 1kB state management used New context api and Proxy which is similar to Vuex.

Feature

🐽 New Context Api used and Api is similar to Vuex

🎃 Tiny size, 1Kb gzipped, no Dependencies

👻 High Performance without optimization because ES6 Proxy

🙀 Great Model and Middleware mechanisms supported

Docs

smox documents

smox新版本增加 model 机制,点我查看文档!

smox新版本增加中间件机制,点我查看文档!

Install

npm i smox -S

Use

smox 新版本支持 model 机制拆分,以下代码默认是 单model,大型项目需要拆分 model,请参阅文档

import React from 'react'
import ReactDOM from 'react-dom'
import App from './app.js'
import { Store, Provider } from 'smox'

const state = {
  count: 2
}

const actions = {
  asyncAdd({ commit }) {
    setTimeout(() => {
      commit('add')
    }, 1000)
  }
}

const mutations = {
  add(state) {
    state.count += 1
  },
  cut(state) {
    state.count -= 1
  }
}

const store = new Store({ state, mutations, actions })

ReactDOM.render(
  <Provider store = {store}>
    <App />
  </Provider>,
  document.getElementById('root')
)

then app.js

import React from 'react'
import { map } from 'smox'

@map({
  state: ['count'],
  mutations: ['add', 'cut'],
  actions: ['asyncAdd']
})
// @map({
//   mutations: { add ,cut },
//   actions: { asyncAdd }
// })
// ↑ Function as import is also OK

class App extends React.Component {
  render() {
    return (
      <div>
        <h1>现在是{this.props.count}</h1>
        <button onClick={this.props.add}>加一</button>
        <button onClick={this.props.cut}>减一</button>
        <button onClick={this.props.asyncAdd}>异步加一</button>
      </div>
    )
  }
}

export default App

if you only SetState , there is also a produce API to optimize performance

import React from 'react'
import { produce } from 'smox'

class App extends React.Component {
  onClick = () => {
    this.setState(
        produce(state => {
            state.count += 1
        })
    )
  }
}

export default App

API

  • store.state

  • store.mutations

  • store.actions

  • store.commit(mutation)

  • store.dispatch(action)

  • store.subscribe(sub)

  • map({ state:[], mutations:[], actions:[] })

  • produce(state,producer)

Demo

Author

License

MIT Inspirated by vuex & immer

smox缩略图

smox's People

Contributors

yisar avatar solarhell avatar

Watchers

James Cloos avatar Joe_Sky avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.