Redux 多对多关系

编程入门 行业动态 更新时间:2024-10-28 09:13:49
本文介绍了Redux 多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试找出一个好的结构来在 Redux 中创建多对多关系.在这个例子中,我们有图片(可以有多个标签)和标签(可以有多个图片).

I'm trying to figure out a good structure to create a many to many relationship in Redux. For this example we have images (which can have multiple tags) and tags (which can have multiple images).

这是在 Redux 中构建它的好方法吗?

Is this a good way to structure it in Redux?

images: { 1: { title: "A bunch of hills" }, 2: { title: "Hiking in a forest" } }, tags: { 1: { title: "landscape" }, 2: { title: "outdoors" } }, imagesTags: { [ image: 1, tag: 1 ], [ image: 1, tag: 2 ], [ image: 2, tag: 2 ] }

这确实意味着我必须创建一个单独的 reducer,这会扰乱我的模块化结构,但我想这始终是拥有相关 reducer 的结果.

It does mean I have to create a separate reducer which messes with my modular structure but I guess that is always going to be the result of having related reducers.

推荐答案

是的,这是规范化"状态的示例.Redux 文档在 Structuring Reducers 部分介绍了规范化.请参阅结构化减速器 - 先决条件概念、结构化Reducers - 规范化状态,以及结构化减速器 - 更新标准化数据.此外,Redux FAQ 在 "如何组织我所在州的嵌套或重复数据?".

Yep, this is an example of a "normalized" state. The Redux docs cover normalization in the Structuring Reducers section. See Structuring Reducers - Prerequisite Concepts, Structuring Reducers - Normalizing State Shape, and Structuring Reducers - Updating Normalized Data. Also, the Redux FAQ discusses normalization in "How do I organize nested or duplicate data in my state?".

除此之外,我非常喜欢使用名为 Redux-ORM 的库来管理我的 Redux 存储中的关系数据.我写了几篇文章讨论我如何使用它:实用 Redux 第 1 部分:Redux-ORM 基础 和 实用 Redux 第 2 部分:Redux-ORM 概念和技术.我的其余Practical Redux"教程系列也展示了这个库的实际应用.

Beyond that, I'm a big fan of using a library called Redux-ORM to manage that relational data in my Redux store. I've written a couple posts that discuss how I use it: Practical Redux Part 1: Redux-ORM Basics and Practical Redux Part 2: Redux-ORM Concepts and Techniques. The rest of my "Practical Redux" tutorial series also shows this library in action.

更多推荐

Redux 多对多关系

本文发布于:2023-10-23 13:27:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   Redux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!