[ ] Redux: API |
React Redux , . , - . API , , , , store, action API, , , action-creators, action-creators props, dispatch props, reducer action , . - .
, : ", , . !" , , , , . - , , Django, . - , store .
, , - .
, , :
, action creator', middleware reducer'.
, , JS , . , API redux-api-middleware
, react-addons-update
, ( ?) normalizr
.
. , , API , . . , , , .
1. :
const schema = {
users: {},
posts: {
author: "users"
}
};
- , ? schema.Entity normalizr. , normalizr, , . normalizr , , entity, :
import {schema} from 'normalizr';
const user = new schema.Entity("users", {});
const post = new schema.Entity("posts", {author: user});
const normalizrSchema = {
users: user,
posts: post,
}
, .
2. actions API.
, - , ? , action redux-api-middleware
, :
const api = {
users: {
endpoint: "mysite.com/api/users/",
types: ['USERS_GET', 'USERS_SUCCESS', 'USERS_FAILURE'],
},
posts: {
endpoint: "mysite.com/api/posts/",
types: ['POSTS_GET', 'POSTS_SUCCESS', 'POSTS_FAILURE'],
}
};
, action , . GET-, method.
3. " " store.
, , - . " " .
const lifetime = {
users: 20000,
posts: 100000
};
:
const config = {schema, api, lifetime};
, "" store 20 , 100 . , , store, , . store.
. store entities timestamp. normalizr entities , - :
const entities = {
posts: {1: {id: 1, content: "content", author: 1}, 2: {id: 2, content: "not content", author: 2}},
users: {1: {id: 1, username: "one"}, 2: {id: 2, username: "two"}}
};
, -, , , -id .
timestamp , id , Date.now()
.
const timestamp = {
posts: {1: 1496618924981, 2: 1496618924981},
users: {1: 1496618924983, 2: 1496618924983}
};
, -, . .