- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个音乐播放器应用程序,并使用 REST API 从各个端点获取轨道(播放列表、特定用户发布的轨道等)
我正在使用 redux 和 normalizr 来管理我的状态。我创建了一个 tracks
实体,用于映射 track
对象及其 id。
简而言之,我的状态看起来有点像这样:
{
playlists: {
123: { id: 123, name: 'foo', tracks: [1, 2, 3] }
},
users: {
me: { name: 'Niek', tracks: [2, 3] },
},
entities: {
tracks: {
1: { name: 'bar', user_favorited: false, ... },
2: { name: 'foo', user_favorited: false, ... },
3: { name: 'bar', user_favorited: true, ... }
}
}
}
到目前为止,这对我来说效果很好。但现在我正在构建通过调用另一个 API 端点来切换轨道的收藏
状态的功能。
此端点在响应中不包含整个 track
资源,但我确实想更新单个 track
的 user_favorited
属性API调用成功后的实体。
实现这一目标的最佳方法是什么?
作为一种解决方法,我现在在 user_favorited
端点调用成功后再次从另一个端点获取整个 track
实体,但我希望找到一个优雅的解决方案没有必要这样做的地方。
最佳答案
将提出两种解决方案:
重新获取数据。
更改实体。
实现此目的的一种方法是创建一个如下所示的实体缩减器:
const tracksReducer = (state, action) => {
// Change the tracks
}
const entitiesReducer = (state, action) => {
let _state = state;
// Store all entities
if (action.payload && action.payload.entities) {
_state = merge({}, state, action.payload.entities);
}
return combineReducers({
tracks: tracksReducer,
})(_state, action);
};
export default { entities: entitiesReducer };
* 上面的代码使用了 lodash 中的 merge
和 redux 中的 combineReducers
Lately I've been also thinking about another structure completely, but haven't tested it nor found anybody else sharing their experiences with it. The idea would be do remove the first level "entities" completely and storing them under the corresponding key. Theoretically it seems to make sense when we start to make changes to entities and would be easier to keep all similar reducers, selectors etc. in a single place.
{
tracks: {
entities: {
1: { name: 'bar', user_favorited: false, ... },
2: { name: 'foo', user_favorited: false, ... },
3: { name: 'bar', user_favorited: true, ... }
},
top3: [1, 2, 3],
somethingAboutTracks: true
},
users: {
entities: {},
me: { name: 'Niek', tracks: [2, 3] }
},
playlists: {
entities: {
123: { id: 123, name: 'foo', tracks: [1, 2, 3] }
}
}
}
关于reactjs - Normalizr + Redux - 更新单个实体的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298766/
我正在规范化一个一级嵌套批处理列表。第一级地段称为主人,嵌套的是奴隶。 // my schema const lot = new schema.Entity('lots'); const lots =
我有一个 API,它使用 fields 属性中的属性提供这样的数据。 { records: [ { id: "123", fields: { author: {
我有一个像这样的嵌套对象数组: var matchs = [ { id: 10689, sport: 'Tennis', players: [
我使用 normalzr 来标准化 Redux 存储中的数据。但是,我不知道如何在标准化数据上创建标准化索引。以下是我在不使用 Normalizr 的情况下解决该问题的方法: var postData
我如何转换一些json数组 [ { "travelExpenseId":11, "tripId":2, "paymentPurpose":"some paym
import { normalize, Schema, arrayOf } from 'normalizr'; var ListA = [ { id:1, te
我正在对从 Api 接收的数据使用 normalizr。当我收到列表时,我会按预期获得规范化实体。 但是当我发送更新一个实体的请求时,我只收到一个包裹在数据信封中的实体,它没有被 normalizr
我需要重组来自服务器 API 的数据以在我的客户端应用程序中使用,我开始了解 normalizr .如果可能,我正在尝试使用这个库。 数据结构如下。 { "history": [{
我需要规范化这些数据,以便我同时拥有一组列表和另一个待办事项。 const data = [ { _id: '1', title: 'List1', todos: [
对于多态模式,例如 Union在 Normalizr 中,对于模式定义和数据: const data = { owner: { id: 1, type: 'user', name: 'Anne' }
如果您有一个规范化的 redux 存储并为实体使用 id,但还有其他额外的唯一标识符并且也需要使用它们,您会怎么做。 例子 { entities: { users: {
我正在构建一个音乐播放器应用程序,并使用 REST API 从各个端点获取轨道(播放列表、特定用户发布的轨道等) 我正在使用 redux 和 normalizr 来管理我的状态。我创建了一个 trac
我知道我的问题的表述很奇怪,但我不知道如何正确表达。我想使用 Redux 的 normalizr 规范化我的数据。让我用代码陈述我的问题。 我有一个如下所示的 api 响应: [ { "n
comments : { byId : { "comment1" : { id : "comment1", author : "
我刚刚开始在 Redux 中使用 normalizr,我被困在我看来像一个简单的问题上,但我可能做错了。所以我想对这样的数组进行规范化: { articles: [ {id: 1, som
考虑传回的 User 对象: { "id": "123", "username": "TestUser", "group": { "id": "324",
我想用 normalizr 规范化我的数据.问题是我的数据中有一个键 (teams),它与其他数据没有关系。 例如: const data = { programs: [{ id:
我想规范化这种格式的数据: [ { id: 3, status: "active", created: "2017-07-03T15
我必须规范化来自服务器的两个响应,并使用 normalizr 将它们存储在我的商店中。第一个回复给了我章节,第二个回复给了我帖子。 版 block 有很多帖子。 一个帖子只能有一个部分。 第一 rea
在使用 Normalizr 后我有一个这样的数组: comments : { byId : { "comment1" : { i
我是一名优秀的程序员,十分优秀!