2.2.0 • Published 5 years ago

@francoisv/react-store v2.2.0

Weekly downloads
51
License
MIT
Repository
-
Last release
5 years ago

react-store

React store

Install

# with yarn
yarn add @francoisv/react-store

# with npm
npm i -S @francoisv/react-store

Todos

First, let's get our dependencies:

import React from 'react'
import store, { withStore } from '@francoisv/react-store'

Now let's create a store array for the todos. For the sake of simplicity, todos are plain strings. We'll start with one todo in the list.

const todos = store.Array('Buy milk')

Now let's just create a list component with it

const TodosView = () => (
  <ul>
    { store.get(todos).map(todo => ( <li key={ todo }>{ todo }</li> )) }
  </ul>
)

We want the component to update whenever todos changes. For that, we'll use the HOC withStore

const Todos = withStore(todos)(TodosView)

Let's add a form to add a new todo.

const newTodo = store.string()

const AddTodo = withStore(newTodo)(() => (
  <form>
    <input
      value={ store.get(newTodo) }
      onChange={ event => store.set(newTodo, event.target.value) }
    />
    <button onClick={ () => store.push(todos, store.get(newTodo)) }>
      Add
    </button>
  </form>
))

Now let's create a way to delete todos when they are done. Let's add a checkbox next to each todo to do that.

const Todo = props => (
  <div>
    <input
      type="checkbox"
      onChange={ () => store.filter(todos, todo => todo !== props.todo) }
    />
    { props.todo }
  </div>
)

We can now call each todo such as:

{ store.get(todos).map(todo => ( <li key={ todo }><Todo todo={ todo } /></li> )) }

Let's say we want to edit the todo. Let's level our Todo component. We'll use the props to create a local store that will be passed in the props

const state = props => ({ nextTodo: store.string() }) 

const Todo = props => {
  const onSave = () => store.map(
    todos,
    todo => {
      // if this current todo, change it with new value
      if (todo === props.todo) {
        return store.get(props.nextTodo)
      }
      return todo
    }
  )
  
  return (
    <div>
      <input
        type="checkbox"
        onChange={ () => store.filter(todos, todo => todo !== props.todo) }
      />
      <input
        value={ store.get(props.nextTodo) }
        onChange={ event => store.set(props.nextTodo, event.target.value) }
      />
      <button onClick={ onSave }>
        Change
      </button>
    </div>
  )
}
2.2.0

5 years ago

2.1.0

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.40

5 years ago

1.0.39

5 years ago

1.0.38

5 years ago

1.0.37

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago