Redux JSON API Module
Redux reducer, actions, action creators, and selectors for interacting with JSON API.
Install
Add it to your reducers.
// reducer.js ;;
Define your API endpoints with axios middle ware
// middleware.js ;;; const client = axios;
NOTE: If you need to add auth headers or take action on api call request/response checkout redux-axios-middleware interceptors
Usage
In our container component we call fetchRecords()
to load the data we need. Notice we use the returned promise from
fetchRecords()
to toggle a loading state.
When fetchRecords()
returns with an error we record the ids, this is a good idea for performance reasons. Storing the
returned id's means we don't need to sort and filter the raw store data whenever we make changes.
// RecordList.js ;;; state = ids: loading: false ; { const fetchRecords = thisprops; this; // fetch the 10 most recently updated records with user } { const ids loading = thisstate; return loading ? <div className="loader" /> : <ul> ids </ul> } const mapDispatchToProps = fetchRecords; null mapDispatchToPropsRecordList
In our child component we use getRecord()
and getRelationship()
to load the item and related user records. Then, we
pass in only the attributes we need. Passing only needed attributes keeps the mapStateToProps flat and simple, reducing
needless rendering
// RecordList.js ;;; const RecordItem = <li>` by `</li>; const mapStateToProps = { const item = ; const user = ; return itemTitle: itemattributestitle userName: userattributesname }; mapStateToPropsRecordList