• This repository has been archived on 31/Dec/2021
  • Stars
    star
    100
  • Rank 340,703 (Top 7 %)
  • Language
    TypeScript
  • Created about 7 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A minimal state store for React component.

mini-store

Deprecated, see use-context-selector for optimized state subscribtion

Travis

A state store for React component.

Motivation

When you want to share a component's state to another one, a commom pattern in React world is lifting state up. But one problem of this pattern is performance, assume we have a component in following hierarchy:

<Parent>
  <ChildA />
  <ChildB />
  <ChildC />
</Parent>

ChildA want to share state with ChildB, so you lifting ChildA's state up to Parent. Now, when ChildA's state changes, the whole Parent will rerender, includes ChildC which should not happen.

Redux do a good job at this situation throgh keeping all state in store, then component can subscribe state's changes, and only connected components will rerender. But redux + react-redux is overkill when you are writing a component library. So I wrote this little library, It's like Redux's store without "reducer" and "dispatch".

Example

See this demo online.

import { Provider, create, connect } from 'mini-store';

class Counter extends React.Component {
  constructor(props) {
    super(props);

    this.store = create({
      count: 0,
    });
  }

  render() {
    return (
      <Provider store={this.store}>
        <div>
          <Buttons />
          <Result />
        </div>
      </Provider>
    )
  }
}

@connect()
class Buttons extends React.Component {
  handleClick = (step) => () => {
    const { store } = this.props;
    const { count } = store.getState();
    store.setState({ count: count + step });
  }

  render() {
    return (
      <div>
        <button onClick={this.handleClick(1)}>+</button>
        <button onClick={this.handleClick(-1)}>-</button>
      </div>
    );
  }
}

@connect((state) => ({ count: state.count }))
class Result extends React.Component {
  render() {
    return (
      <div>{this.props.count}</div>
    );
  };
}

API

create(initialState)

Creates a store that holds the state. initialState is plain object.

<Provider store>

Makes the store available to the connect() calls in the component hierarchy below.

connect(mapStateToProps)

Connects a React component to the store. It works like Redux's connect, but only accept mapStateToProps. The connected component also receive store as a prop, you can call setState directly on store.

License

MIT

More Repositories

1

jquery-jsonview

[UNMAINTAINED]View JSON in a more readable format
CoffeeScript
488
star
2

waque

[不再维护]同步本地 Markdown 文档到语雀
TypeScript
244
star
3

remix-routes

Typesafe routing for your Remix apps.
TypeScript
206
star
4

reactive.macro

Reduce the React boilerplate.
TypeScript
172
star
5

react-with-hooks

[OUTDATED]Ponyfill for the React Hooks API (Support RN)
JavaScript
152
star
6

redux-modal

Redux based modal
TypeScript
105
star
7

vs-codesandbox

Bring CodeSandbox into your favorite editor
TypeScript
81
star
8

camas

Minimal React authorization library
TypeScript
66
star
9

tmuxrc

My configuration for tmux.
Shell
65
star
10

webpack-less-theme-plugin

Inject less variables to your less file. Support HMR.
JavaScript
26
star
11

styled-antd

Proof of concept using styled-components with antd
JavaScript
25
star
12

react-context-io

Share state with ease.
TypeScript
22
star
13

hexdocset

Convert hex doc to Dash.app's docset format.
Elixir
21
star
14

babel-plugin-reactive

Make React Reactive Again!
TypeScript
18
star
15

rx-hackernews

HackerNews clone built with React/Redux/redux-observable.
JavaScript
17
star
16

dotbox

Backup your dotfiles to dropbox and restore them easily.
Ruby
10
star
17

antd.macro

A babel macro that helps you import antd's style.
TypeScript
9
star
18

antd-rtl

antd rtl demo
CSS
6
star
19

star

下载豆瓣红心歌曲
Ruby
6
star
20

toby

Ruby 版淘宝 API SDK
Ruby
6
star
21

redux-polymorphic

Reuse your reduers
JavaScript
5
star
22

umi-routes-drawer

TypeScript
5
star
23

yesmeck

5
star
24

HelloSketch

Swift
4
star
25

douban_fm_hotkey

Douban FM 全局快捷键
JavaScript
4
star
26

react-full-viewport

A React component that sets its children to full viewport.
TypeScript
4
star
27

antd-dash

JavaScript
3
star
28

neotable

JavaScript
3
star
29

formsy-react-inputs

A set of formsy-react inputs without any fancy.
JavaScript
3
star
30

git-phpcs-hooks

Shell
2
star
31

github-comment-webhook

Ruby
2
star
32

vim-tips.org

The source for vim-tips.org.
Ruby
2
star
33

is-test

Next.js template for CodeSandbox Projects
TypeScript
2
star
34

monkey-lang

A Monkey implementation in Rust for learning purpose.
Rust
2
star
35

bookmarklets

Chrome bookmarklet extension
JavaScript
2
star
36

babel-preset-antd

JavaScript
2
star
37

git-graph

Graph git commits.
CSS
1
star
38

tree-transfer

JavaScript
1
star
39

react-vue-antd-demo

JavaScript
1
star
40

babel-spread-undefined

JavaScript
1
star
41

huami.ex

A CLI version of flower password writing in Elixir.
Elixir
1
star
42

enzyme-set-props-bug

JavaScript
1
star
43

hello-codesandbox-projects

Remix template for CodeSandbox Projects
TypeScript
1
star
44

bug-24

JavaScript
1
star
45

antd-enzyme

[DEPRECATED]
JavaScript
1
star
46

hello-form-ios

Next.js template for CodeSandbox Projects
TypeScript
1
star
47

vimrc

My very personal vimrc
Vim Script
1
star
48

Sublime-IDE

My configuration for using Sublime Text as an IDE.
Python
1
star
49

huami.rb

A CLI version of flower password.
Ruby
1
star
50

hubot-tianqi

Hubot 天气查询
CoffeeScript
1
star
51

raycast-extensions

My Raycast extensions
TypeScript
1
star
52

hubot-xiexie

A hubot script that accepts your 谢谢
CoffeeScript
1
star
53

remax-suspense-demo

JavaScript
1
star
54

github-slideshow

A robot powered training repository 🤖
HTML
1
star
55

react-oom

JavaScript
1
star
56

babel-plugin-resolve-es6-module

JavaScript
1
star
57

tips.vim

Show vim tips everytime when you enter vim.
Vim Script
1
star