GithubHelp home page GithubHelp logo

gnbaron / redux-zero Goto Github PK

View Code? Open in Web Editor NEW

This project forked from redux-zero/redux-zero

1.0 1.0 0.0 351 KB

A lightweight state container based on Redux

License: Other

JavaScript 10.81% TypeScript 89.19%

redux-zero's Introduction

redux zero logo

A lightweight state container based on Redux

Read the intro blog post


build npm downloads license dependencies

  • Single store
  • No reducers
  • Less boilerplate
  • No PropTypes
  • Smaller and simpler than redux
  • Written in TypeScript

Installation

To install the stable version:

npm install --save redux-zero

This assumes that you’re using npm with a module bundler like Webpack

How

ES2015+ or TypeScript:

import createStore from "redux-zero"
import { Provider, connect } from "redux-zero/react"

CommonJS:

const createStore = require("redux-zero")
const { Provider, connect } = require("redux-zero/react")

UMD:

<script src="https://unpkg.com/redux-zero/dist/redux-zero.min.js"></script>
<script src="https://unpkg.com/redux-zero/react/index.min.js"></script>

Example

Let's make an increment/decrement simple application with React:

First, create your store. This is where your application state will live:

/* store.js */
import createStore from "redux-zero";

const initialState = { count: 1 };
const store = createStore(initialState);

export default store;

Then, create your actions. This is where you change the state from your store:

/* actions.js */
export const actions = store => ({
  increment: state => ({ count: state.count + 1 }),
  decrement: state => ({ count: state.count - 1 })
})

By the way, because the actions are bound to the store, they are just pure functions :)

Now create your component. With Redux Zero your component can focus 100% on the UI and just call the actions that will automatically update the state:

/* Counter.js */
import React from "react";
import { connect } from "redux-zero/react";

import actions from "./actions";

const mapToProps = ({ count }) => ({ count });

export default connect(mapToProps, actions)(({ count, increment, decrement }) => (
  <div>
    <h1>{count}</h1>
    <div>
      <button onClick={decrement}>decrement</button>
      <button onClick={increment}>increment</button>
    </div>
  </div>
));

Last but not least, plug the whole thing in your index file:

/* index.js */
import React from "react";
import { render } from "react-dom";
import { Provider } from "redux-zero/react";

import store from "./store";

import Counter from "./Counter";

const App = () => (
  <Provider store={store}>
    <Counter />
  </Provider>
);

render(<App />, document.getElementById("root"));

Here's the full version: https://codesandbox.io/s/n5orzr5mxj

More on examples folder

Inspiration

Redux Zero was based on this gist by @developit

Roadmap

  • Add more use case examples (including unit tests, SSR, etc)
  • Add Preact bindings

Tools

These are unofficial tools, maintained by the community:

Docs

redux-zero's People

Contributors

chevsky avatar dmmulroy avatar gnbaron avatar guilhermefloriani avatar malbernaz avatar matheusml avatar ricardo-devis-agullo avatar sidkwok avatar uselessdev avatar

Stargazers

 avatar

Watchers

 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.