7.0.0-beta.0 • Published 5 years ago

@redux-model/react v7.0.0-beta.0

Weekly downloads
81
License
MIT
Repository
github
Last release
5 years ago

Redux Model is created to enhance original redux framework, which has complex development flow and lots of template fragments.

License GitHub Workflow Status (branch) Codecov

Features

  • Less code and higher efficiency
  • Modify reducer by MVVM
  • Absolutely 100% static type checking with typescript
  • Trace loading status for each request action
  • Support react hooks

Installation

React or React-Native

npm install @redux-model/react redux react-redux

Taro v3

npm install @redux-model/taro redux react-redux

Vue v3

npm install @redux-model/vue redux

Others

  • For taro < 3, install @redux-model/taro@6.9.2 instead
  • For vue < 3, install @redux-model/vue@6.9.2 instead

Define Model

interface Response {
  id: number;
  name: string;
}

interface Data {
  counter: number;
  users: Partial<{
    [key: string]: Response;
  }>;
}

class TestModel extends Model<Data> {
    increase = this.action((state) => {
        state.counter += 1;
    });

    getUser = $api.action((id: number) => {
        return this
            .get<Response>(`/api/user/${id}`)
            .onSuccess((state, action) => {
                state.counter += 1;
                state.users[id] = action.response;
            });
    });

    protected initReducer(): Data {
        return {
            counter: 0,
            users: {},
        };
    }
}

export const testModel = new TestModel();

With React Hooks

import React, { FC } from 'react';

const App: FC = () => {
    const counter = testModel.useData((data) => data.counter);
    const loading = testModel.getUser.useLoading();

    const increase = () => {
        testModel.increase();
        testModel.getUser(3);
    };

    return (
        <button onClick={increase}>
            {loading ? 'Waiting...' : `You clicked ${counter} times`}
        </button>
    );
};

export default App;

With Redux connect

import React, { Component } from 'react';
import { connect } from 'react-redux';

type Props = ReturnType<typeof mapStateToProps>;

class App extends Component<Props> {
    increase() {
        testModel.increase();
        testModel.getUser(3);
    }

    render() {
        const { loading, counter } = this.props;
        return (
            <button onClick={this.increase}>
                {loading ? 'Waiting...' : `You clicked ${counter} times`}
            </button>
        );
    }
}

const mapStateToProps = () => {
    return {
        counter: testModel.data.counter,
        loading: testModel.getUser.loading,
    };
};

export default connect(mapStateToProps)(App);

With Vue

<template>
  <button @click="increase">
    {{loading ? 'Waiting...' : `You clicked ${counter} times`}}
  </button>
</template>

<script>
export default {
  name: 'HelloWorld',
  methods: {
    increase() {
      testModel.increase();
      testModel.getUser(3);
    },
  },
  computed: {
    counter() {
      return testModel.data.counter;
    },
    loading() {
        return testModel.getUser.loading;
    },
  },
};
</script>

Demos

Documents

Here is the document


Feel free to use it and welcome to send PR to me.

9.0.6

4 years ago

9.0.5

4 years ago

9.1.0-alpha.1

5 years ago

9.1.0-alpha.0

5 years ago

9.0.4

5 years ago

9.0.3

5 years ago

9.0.2

5 years ago

9.0.1

5 years ago

9.0.0

5 years ago

9.0.0-alpha.5

5 years ago

9.0.0-alpha.4

5 years ago

9.0.0-alpha.3

5 years ago

9.0.0-alpha.2

5 years ago

9.0.0-alpha.1

5 years ago

9.0.0-alpha.0

5 years ago

8.3.0-alpha.0

5 years ago

8.2.2

5 years ago

8.2.2-alpha.2

5 years ago

8.2.2-alpha.1

5 years ago

8.2.2-alpha.0

5 years ago

8.2.1

5 years ago

8.2.0

5 years ago

8.1.3

5 years ago

8.1.2

5 years ago

8.1.1

5 years ago

8.1.0

5 years ago

8.0.2

5 years ago

8.0.1

5 years ago

8.0.1-alpha.4

5 years ago

8.0.1-alpha.3

5 years ago

8.0.1-alpha.2

5 years ago

8.0.1-alpha.1

5 years ago

8.0.0-alpha.0

5 years ago

8.0.0

5 years ago

7.3.1-alpha.0

5 years ago

7.3.0

5 years ago

7.2.1

5 years ago

7.2.0

5 years ago

7.2.0-alpha.0

5 years ago

7.1.1-alpha.0

5 years ago

7.1.0

5 years ago

7.0.1

5 years ago

7.0.1-alpha.0

5 years ago

7.0.0

5 years ago

7.0.0-beta.14

5 years ago

7.0.0-rc.0

5 years ago

7.0.0-rc.1

5 years ago

7.0.0-beta.13

5 years ago

7.0.0-beta.12

5 years ago

7.0.0-beta.11

5 years ago

7.0.0-beta.10

5 years ago

7.0.0-beta.9

5 years ago

7.0.0-beta.6

5 years ago

7.0.0-beta.7

5 years ago

7.0.0-beta.4

5 years ago

7.0.0-beta.2

5 years ago

7.0.0-beta.3

5 years ago

7.0.0-alpha.7

5 years ago

7.0.0-beta.0

5 years ago

7.0.0-alpha.6

5 years ago

7.0.0-alpha.4

5 years ago

7.0.0-alpha.1

5 years ago