Giter Site home page Giter Site logo

decx's Introduction

The simplest React.js state management library which coexists with Redux

Decorator + Reducer

ALERT

You need to download babel plugin @babel/plugin-proposal-decorators

//in webpack.config.js

//the order should be identical
 [ "@babel/plugin-proposal-decorators", {"legacy":true}],
 ["@babel/plugin-proposal-class-properties", { "loose": true }]

    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                  loader: 'babel-loader',
                  options: {
                    presets: ['@babel/preset-env',"@babel/preset-react"],
                    plugins:["@babel/plugin-proposal-object-rest-spread",
                    [
                      "@babel/plugin-transform-runtime",
                      {
                        "helpers": true
                      }
                    ],
                    "babel-plugin-styled-components",
                    [ "@babel/plugin-proposal-decorators", {"legacy":true}],
                    ["@babel/plugin-proposal-class-properties", { "loose": true }],
                    "@babel/plugin-syntax-jsx",
                    "@babel/plugin-transform-classes"
                    ],
                  }
                }
            }
        ]
      }
//in index.js
import {Store, DecxProvider,combinereducers} from "decx";

//custom reducers
const firstReducer = (state = 0, action) => {
    switch(action.type){
        case "INC":
        return state + payload;
        default:
        return state;
    }
}

const secondReducer = (state = "", action) => {
       switch(action.type){
        case "SCREAM":
        return `${state}!!!!`;
        default:
        return state;
    }
}

const reducers = combinereducers({
    number:firstReducer,
    text:secondReducer
})

const store = Store(reducers)

ReactDOM.render(
    <DecxProvider store={store}>
        <App/>
     </DecxProvider>
, document.getElementById('root'));
//in App.jsx
import React, {Component} from 'react';
import {listen,event,connect} from 'decx';
import AppChild from '..////'

@connect()
class AppContainer extends Component{
    //dispatch an action
    @event
    onClick = () => {
        //pass an object like redux action creator
        return {type:"INC",payload:5}
    }
    //listen a state change which happens in child component
    @listen
    renderText = (state) => {
      //if state is empty, render nothing
     if(Object.keys(state).length === 0) return   
     const {text} = state;
     return <p>{text}</p>
    }

    render(){
        return(
            <div>
                <button onClick={this.onClick}>Click to increase number</button>
                <AppChild/>
                {this.renderText()}
            </div>
        )
    }
}
//in AppChild.jsx
import React, {Component} from 'react';
import {listen,event,connect} from 'decx';

@connect()
class AppChild extends Component{
    //dispatch an action
    @event
    onClick = () => {
        //pass an object like redux action creator
        return {type:"SCREAM"}
    }
    //listen a state change which happens in parent component
    @listen
    rederNumber = (state) => {
      //if state is empty, render nothing
     if(Object.keys(state).length === 0) return   
     const {number} = state;
     return <p>{number}</p>
    }

    render(){
        return(
            <div>
                <button onClick={this.onClick}>Click to change text</button>
                {this.rederNumber()}
            </div>
        )
    }
}

export default AppChild

decx's People

Watchers

javascript-ninja avatar

decx's Issues

Is this dead?

Do you mind dropping the name? I just want it because I have a friend who's nick is decx, want to make a new package with his name.

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.