1.0.30 • Published 7 years ago

react-datadeps v1.0.30

Weekly downloads
42
License
MIT
Repository
github
Last release
7 years ago

react-datadeps

Manage data dependencies with stateless components

1.- Create a stateless component

interface Props {
    id: string;
    myData: string;
    moreData: string;
    myRefresh: () => void;
}
class MyComponent extends React.Component<Props, {}> {
    render() {
        return <div>
            {this.props.myData}
            {this.props.moreData}
            <button onClick={this.props.myRefresh} >Refresh!</button>
        </div>
    }
}

2.- mapPropsToThunks

import { mapPropsToThunks, PropDependencies } from "react-datadeps";
import { myService, otherService } from "./myService";

const deps: PropDependencies<Props> = {
    myData: {
        query: async (props) => await myService(props.id), //query can be an async function
        async: true, //true to await the query result
        params: ["id"] //when props.id changes, refresh this query
    },
    moreData: {
        query: async (props) => await otherService(props.myData), //neasted data dependency
        async: true,
        params: ["myData"]
    },
    myRefresh: {
        query: (props, refresh) => () => refresh() //Inner component can fire a data refresh
    }
};

3.- Consume the component

//If id changes, myData and moreData will be refreshed
<MyComponent2 id="42" />
1.0.30

7 years ago

1.0.29

7 years ago

1.0.28

7 years ago

1.0.27

7 years ago

1.0.26

7 years ago

1.0.25

7 years ago

1.0.23

7 years ago

1.0.22

7 years ago

1.0.21

7 years ago

1.0.20

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago