0.8.0-alpha8 • Published 6 years ago

morpha-store v0.8.0-alpha8

Weekly downloads
4
License
MIT
Repository
-
Last release
6 years ago

Morpha-Store

A Redux Store for Morpha.

Guide

import React from 'react'
import ReactDOM from 'react-dom'
import { fetchList } from './apis'

import React from 'react'
import ReactDOM from 'react-dom'
import { fetchList } from './apis'

export default function morphaDemo({ store }) {
  let nextId = 0
  return {
    store: {
      initialState: {
        todos: [],
        other: 'other',
      },
      get: {
        length: ({ state }) => state.todos.length,
      },
      set: {
        addTodo({ state }, text, id) {
          return {
            ...state,
            todos: state.todos.concat({
              id: id || nextId ++,
              text,
            }),
          }
        },
        delTodo({ state }, id) {
          return {
            ...state,
            todos: state.todos.filter(todo => todo.id !== id),
          }
        },
      },
      action: {
        // 不并发...
        async fetchTodoList() {
          const dataList = await fetchList()
          dataList.forEach((data) => store.set('addTodo', data.text, data.id))
          return true
        },
      },
    },
    bootstrap() {
      store.setState({ todos: [{ id: 'init', text: 'abc' }] })
      // fetch action
      store.execAction('fetchTodoList')
    },
    render() {
      function mergeToProps({ state, setters, getters }) {
        return {
          todos: state.todos,
          addTodo: setters.addTodo,
          delTodo: setters.delTodo,
          length: getters.length(),
        }
      }
      // subscribe action
      const Container = store.connect(mergeToProps)(function TodosComponent({ todos, addTodo, delTodo, length }) {
        return (
          <div>
            <div>
              Totoal: {length}
            </div>
            <ul>
              {todos.map(todo => <li key={todo.id}>{todo.id}:{todo.text} <button onClick={() => delTodo(todo.id)}>Delete</button></li>)}
            </ul>
            <button onClick={() => addTodo('a new todo')}> AddTodo</button>
          </div>
        )
      })
      const el = document.createElement('div')
      document.body.appendChild(el)
      ReactDOM.render(<Container />, el)
    },
  }
}
0.8.0-alpha8

6 years ago

0.8.0-alpha7

6 years ago

0.8.0-alpha6

6 years ago

0.8.0-alpha5

6 years ago

0.8.0-alpha4

6 years ago

0.8.0-alpha3

6 years ago

0.8.0-alpha2

6 years ago

0.8.0-alpha

6 years ago

0.7.1-alpha9

7 years ago

0.7.1-alpha8

7 years ago

0.7.1-alpha7

7 years ago

0.7.1-alpha6

7 years ago

0.7.1-alpha5

7 years ago

0.7.1-alpha4

7 years ago

0.7.1-alpha3

8 years ago

0.7.1-alpha2

8 years ago

0.7.1-alpha1

8 years ago

0.7.1-alpha0

8 years ago

0.7.0

8 years ago

0.6.0

8 years ago

0.5.6

8 years ago

0.5.5

8 years ago

0.5.4

8 years ago

0.5.2

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.11

8 years ago

0.4.10

8 years ago

0.4.9

8 years ago

0.4.8

8 years ago

0.4.7

8 years ago

0.4.6

8 years ago

0.4.5

8 years ago

0.4.4

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago