• Stars
    star
    1,464
  • Rank 32,122 (Top 0.7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated almost 2 years ago

Reviews

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

Repository Details

🍫 the simplest way to manage state

🧐   Why?

Forget about actions, connections, reducers and a lot of boilerplate to create and manage states. With reworm you can create and manage state as simple as on the image above.

Todo List Example

💻   Install and Usage

Install reworm using your package manager

$ yarn add reworm

Then just wrap your app with our Provider, create your new state and use it!

import React from 'react'
import { Provider, create } from 'reworm'

const { get } = create('userStore', { name: 'John' })

const App = () => (
  <Provider>
    <div>{get(s => s.name)}</div>
  </Provider>
)

Change your state easily

Instead of defining actions or something else to change your state, with reworm you just need to use the set method like that:

import React from 'react'
import { Provider, create } from 'reworm'

const { set, get } = create('userStore', { name: 'John' })

class App extends React.Component {
  componentDidMount() {
    set(prev => ({ name: 'Peter' + prev.name }))
  }
  render() {
    return (
      <Provider>
        <div>{get(s => s.name)}</div>
      </Provider>
    )
  }
}

Using selectors

Selectors are good because they prevent you from duplicating code. With it you can just create some functions and use them throughout your application.

import React from 'react'
import { Provider, create } from 'reworm'

const { select } = create('userStore', { list: ['Peter', 'John'] })

const johnSelector = select(state =>
  state.list.find(user => user.includes('John'))
)

const App = () => (
  <Provider>
    <div>{johnSelector(user => user)}</div>
  </Provider>
)

Listening state changes

If you want to listen changes on your state you can use subscribe():

import React from 'react'
import { Provider, create } from 'reworm'

const user = create('userStore')

class App extends Component {
  state = {
    name: 'John'
  }

  componentDidMount() {
    user.subscribe(name => this.setState({ name }))
    user.set('Michael')
  }

  render() {
    return <div>Hello {this.state.name}</div>
  }
}

Hooks

If you want to use hooks you can use the useReworm:

import React, { useEffect } from 'react'
import { Provider, create, useReworm } from 'reworm'

const store = create('userStore', { name: 'John' })

const App = () => {
  const { get, set } = useReworm('userStore')
  useEffect(() => {
    set(prev => ({ name: 'Peter' + prev.name }))
  }, []);

  return (
    <Provider>
      <div>{get(s => s.name)}</div>
    </Provider>
  )
}

🔎   API

create<T>(storeName: string, initial?: T): State

Create a new state

get((state: T) => React.ReactNode)

Use this method to access your state

set((state: T | (prevState: T) => T) => T)

Use this method to set your state

select<S = any>(selector: (state: T) => S) => (fn: GetFn<T>) => React.ReactNode

Create selectors that can be used with your state and avoid repeating code.

subscribe: (fn: SubscribeFn<T>) => () => void

Use this method to listen state changes

📝   Typings

type PrevState<T> = (prevState: T) => T
type GetFn<T> = (state: T) => React.ReactNode
type SubscribeFn<T> = (state: T) => any

interface State<T> {
  get: (fn: GetFn<T>) => React.ReactNode
  set: (next: T | PrevState<T>) => void
  select: <S = any>(
    selector: (state: T) => S
  ) => (fn: GetFn<S>) => React.ReactNode
  subscribe: (fn: SubscribeFn<T>) => () => void
}

function create<T>(storeName: string, initial: T) => State<T>

🕺   Contribute

If you want to contribute to this project, please see our Contributing Guide !

More Repositories

1

react-adopt

😎 Compose render props components like a pro
TypeScript
1,681
star
2

micro-router

🚉 A tiny and functional router for Zeit's Micro
JavaScript
621
star
3

react-video

🎞 React component to load video from Vimeo or Youtube across any device.
JavaScript
273
star
4

react-simpletabs

Just a simple tabs component built with React
JavaScript
188
star
5

algorithms-with-es6

Just a bunch of algorithms using Javascript with ES6
JavaScript
161
star
6

reicons

💅 Bundle your SVG into a fully customized React components
JavaScript
113
star
7

gatsby-starter-docz

📝 Gatsby starter with Docz and a blog for your documentation
JavaScript
90
star
8

frontend-styleguide

Keep your code clean, legible and beautiful!
CSS
69
star
9

spacefold

Use Pub/Sub pattern inside your React applications easily
TypeScript
61
star
10

oneoften

Tips, tricks, tutoriais and many things about "How building a Large Scale application with Javascript"
44
star
11

yarn-workspaces-example

Sample monorepo project using new Yarn feature called Workspaces
JavaScript
41
star
12

which-licenses-i-have

📝 Learn about the licenses around your package
JavaScript
29
star
13

docz-plugin-react-native

Plugin that allow you to use React Native with docz
JavaScript
21
star
14

storz

Global state machines in an easy way
TypeScript
21
star
15

shazam

⚡️ An opinionated and usefull react app management
JavaScript
17
star
16

hacker-news-es6

Hacker News feed built with ECMAScript 6 and jQuery
JavaScript
17
star
17

vitejs-boilerplate

ViteJS boilerplate with TailwindCSS, React Router v6, Typescript and more.
TypeScript
15
star
18

libundler

JavaScript
14
star
19

nextjs-boilerplate

NextJS boilerplate with some cool stack
TypeScript
12
star
20

promiseJS.br

Tradução do site http://www.promisejs.org/
CSS
11
star
21

certifyJS

NodeJS module that generate a course certificate in PDF
JavaScript
9
star
22

eleicoes2022

Estudos analítico encima dos dados do TSE sobre o resultado das Eleições 2022 no Brasil
7
star
23

notion-todo

Todo app made using the Notion API
JavaScript
6
star
24

xresource

TypeScript
6
star
25

reason-todo-example

Just a simple todo app built with ReasonML
OCaml
5
star
26

to-titlelize

NodeJS module to format string in titlelize
JavaScript
4
star
27

pedronauck.com

My website 🔥
JavaScript
3
star
28

jarvis

Central de conteúdo sobre desenvolvimento da GoNorth
2
star
29

builder-skeleton

Just a test for Stackblitz api
HTML
2
star
30

create-dataset

Experiment recreating React Context using plain Javascript
JavaScript
2
star
31

nvim

My current Neovim configuration
Lua
2
star
32

typescript-with-docz-example

Just a simple test
TypeScript
2
star
33

react-grocery-list

This is a sample Grocery List application to test ReactJS + Gulp + Browserify
JavaScript
2
star
34

xstate-fp

Just another approach to write state machines for XState
1
star
35

zmk-config

My zmk configuration
1
star
36

fusebox-preact-example

Sample application using FuseBox and Preact
JavaScript
1
star
37

docz-plugin-svgr

Use svgr as loader for svg images
TypeScript
1
star
38

teste-repo

Teste de repositório
1
star
39

datocms-blog-demo-5245

JavaScript
1
star
40

astronvim_config

Astro vim config
Lua
1
star
41

mediator

Mediator pattern applied on React
TypeScript
1
star
42

csb-xut4hw

HTML
1
star