简体   繁体   English

使用 Redux 工具包的设计模式

[英]Design Pattern with Redux Toolkit

I am struggling on how to implement redux toolkit on my project.我正在努力解决如何在我的项目中实施 redux 工具包。 I have an API that fetches Setup objects.我有一个获取Setup对象的 API。 Each of these objects is quite big with a lot of properties.这些对象中的每一个都非常大,具有很多属性。 My initial approach was to use RTK to fetch all of the objects but only with the name and id props.我最初的方法是使用 RTK 获取所有对象,但仅使用nameid道具。 Then, have a separate slice for a single full Setup object that will store all the props of the selected Setup .然后,为单个完整的Setup object 设置一个单独的切片,该切片将存储所选Setup的所有道具。 This is causing a lot of issues for me as state is not loaded appropriately.这给我带来了很多问题,因为 state 没有正确加载。 The current workflow looks as following.当前的工作流程如下所示。 First, load all Setups .首先,加载所有Setups Then, through a dropdown select a single Setup .然后,通过下拉 select 单个Setup Then call an RTK query to fetch all the props of the Setup .然后调用 RTK 查询来获取Setup的所有道具。 Then, load all the props into the slice.然后,将所有道具加载到切片中。 And finally, display the props into a component.最后,将道具显示到组件中。

Do you have any suggestions on how to implement this feature in a more simple and functional way?您对如何以更简单和实用的方式实现此功能有任何建议吗? I have thought about loading all the props directly instead of just the id and name.我考虑过直接加载所有道具,而不仅仅是 id 和 name。 Or have two RTKs.或者有两个 RTK。 One with all the Setups and then another cached RTK for a single Setup .一个包含所有 Setups ,然后另一个缓存 RTK 用于单个Setup One thing to keep in mind is that I plan to then perform different operations on that single selected Setup such as changing the name, filters, etc.要记住的一件事是,我计划然后对单个选定的设置执行不同的操作,例如更改名称、过滤器等。

This is a sample of the API I call to get all the Setup (only name and id ):这是 API 的示例,我调用它来获取所有Setup (只有nameid ):

[
    {
        "date": "Sat, 02 Jul 2022 00:14:50 GMT",
        "default": true,
        "id": "62bf8dfab60731051bcf77dd",
        "name": "Default 0"
    },
    {
        "date": "Thu, 30 Jun 2022 22:05:59 GMT",
        "default": false,
        "id": "62be1e47d4c5f49eb95cd06d",
        "name": "Filter Setup"
    }
]

This is a sample of all the props of a single Setup :这是单个Setup的所有道具的示例:

{
    "_id": {
        "$oid": "62be1e47d4c5f49eb95cd06d"
    },
    "name": "Filter Setup",
    "default": false,
    "filters": [
        {
            "id": {
                "$oid": "62bf8d03b60731051bcf77db"
            },
            "name": "RRR greater than 1"
        },
        {
            "id": {
                "$oid": "62bf8d09b60731051bcf77dc"
            },
            "name": "RRR greater than 1"
        }
    ],
    "state": {
        "schema": {
            "fields": [
                {
                    "name": "index",
                    "type": "integer"
                },
                {
                    "name": "#",
                    "type": "number"
                },
                {
                    "name": ".m_RRR",
                    "type": "number"
                },
                {
                    "name": ".r_Result",
                    "type": "number"
                },
                {
                    "name": ".r_Result 50% Candle SL",
                    "type": "number"
                },
                {
                    "name": ".m_Entry Candle",
                    "type": "string"
                },
                {
                    "name": ".m_Zone Touch #",
                    "type": "number"
                },
                {
                    "name": ".m_Zone Origin",
                    "type": "number"
                },
                {
                    "name": ".m_Re-entry @ Other Level",
                    "type": "string"
                },
                {
                    "name": ".m_Fast Into Profits",
                    "type": "string"
                },
                {
                    "name": ".m_Trade @ Range Over",
                    "type": "string"
                },
                {
                    "name": ".m_Checked",
                    "type": "string"
                },
                {
                    "name": ".p",
                    "type": "string"
                }
            ],
            "primaryKey": [
                "index"
            ],
            "pandas_version": "0.20.0"
        },
        "data": [
            {
                "index": 0,
                "#": 1.0,
                ".m_RRR": 3.37,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Hammer",
                ".m_Zone Touch #": 2.0,
                ".m_Zone Origin": 1.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 1,
                "#": 2.0,
                ".m_RRR": 2.43,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Hammer",
                ".m_Zone Touch #": 3.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 2,
                "#": 3.0,
                ".m_RRR": 2.29,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Engulfing",
                ".m_Zone Touch #": 3.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 3,
                "#": 4.0,
                ".m_RRR": 2.97,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Engulfing",
                ".m_Zone Touch #": 3.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 4,
                "#": 5.0,
                ".m_RRR": 3.48,
                ".r_Result": null,
                ".r_Result 50% Candle SL": null,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 4.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 5,
                "#": 6.0,
                ".m_RRR": 3.22,
                ".r_Result": 3.22,
                ".r_Result 50% Candle SL": 3.22,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 4.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "Yes",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 6,
                "#": 7.0,
                ".m_RRR": 1.98,
                ".r_Result": 0.0,
                ".r_Result 50% Candle SL": 0.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 5.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 7,
                "#": 8.0,
                ".m_RRR": 4.8,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 2.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 8,
                "#": 9.0,
                ".m_RRR": 4.59,
                ".r_Result": 4.59,
                ".r_Result 50% Candle SL": 4.59,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 3.0,
                ".m_Zone Origin": 2.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "Yes",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 9,
                "#": 10.0,
                ".m_RRR": 1.71,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 2.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 10,
                "#": 11.0,
                ".m_RRR": 2.77,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 2.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 11,
                "#": 12.0,
                ".m_RRR": 2.15,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Hammer",
                ".m_Zone Touch #": 3.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 12,
                "#": 13.0,
                ".m_RRR": 1.71,
                ".r_Result": 1.71,
                ".r_Result 50% Candle SL": 1.71,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 2.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "Yes",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 13,
                "#": 14.0,
                ".m_RRR": 6.0,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 4.0,
                ".m_Zone Origin": 1.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "Yes",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 14,
                "#": 15.0,
                ".m_RRR": 6.48,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 4.0,
                ".m_Zone Origin": 1.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "Yes",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 15,
                "#": 16.0,
                ".m_RRR": 5.07,
                ".r_Result": 4.67,
                ".r_Result 50% Candle SL": 4.67,
                ".m_Entry Candle": "Hammer",
                ".m_Zone Touch #": 2.0,
                ".m_Zone Origin": 1.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 16,
                "#": 17.0,
                ".m_RRR": 1.86,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 2.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 17,
                "#": 18.0,
                ".m_RRR": 3.87,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Hammer",
                ".m_Zone Touch #": 8.0,
                ".m_Zone Origin": 1.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 18,
                "#": 19.0,
                ".m_RRR": 1.97,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 3.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 19,
                "#": 20.0,
                ".m_RRR": 3.25,
                ".r_Result": 3.07,
                ".r_Result 50% Candle SL": 3.07,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 3.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "Yes",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 20,
                "#": 21.0,
                ".m_RRR": 4.15,
                ".r_Result": 0.0,
                ".r_Result 50% Candle SL": 0.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 3.0,
                ".m_Zone Origin": 2.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 21,
                "#": 22.0,
                ".m_RRR": 2.44,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 3.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 22,
                "#": 23.0,
                ".m_RRR": 3.9,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 4.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "Yes",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 23,
                "#": 24.0,
                ".m_RRR": 1.92,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 4.0,
                ".m_Zone Origin": 0.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            },
            {
                "index": 24,
                "#": 25.0,
                ".m_RRR": 2.66,
                ".r_Result": -1.0,
                ".r_Result 50% Candle SL": -1.0,
                ".m_Entry Candle": "Spinning",
                ".m_Zone Touch #": 4.0,
                ".m_Zone Origin": 1.0,
                ".m_Re-entry @ Other Level": "No",
                ".m_Fast Into Profits": "No",
                ".m_Trade @ Range Over": "No",
                ".m_Checked": "No",
                ".p": "AUDUSD"
            }
        ]
    },
    "notes": "",
    "author": {
        "$oid": "6175d1ad7d0ea08fbd85cc2c"
    },
    "documentId": {
        "$oid": "62bdb1e4acbe566f0779271d"
    },
    "date_created": {
        "$date": 1656626759825
    },
    "options": [
        {
            "id": ".m_RRR",
            "name": "RRR",
            "type": "number"
        },
        {
            "id": ".m_Entry Candle",
            "name": "Entry Candle",
            "type": "string",
            "values": [
                "Hammer",
                "Engulfing",
                "Spinning"
            ]
        },
        {
            "id": ".m_Zone Touch #",
            "name": "Zone Touch #",
            "type": "number"
        },
        {
            "id": ".m_Zone Origin",
            "name": "Zone Origin",
            "type": "number"
        },
        {
            "id": ".m_Re-entry @ Other Level",
            "name": "Re-entry @ Other Level",
            "type": "string",
            "values": [
                "No"
            ]
        },
        {
            "id": ".m_Fast Into Profits",
            "name": "Fast Into Profits",
            "type": "string",
            "values": [
                "No",
                "Yes"
            ]
        },
        {
            "id": ".m_Trade @ Range Over",
            "name": "Trade @ Range Over",
            "type": "string",
            "values": [
                "No",
                "Yes"
            ]
        },
        {
            "id": ".m_Checked",
            "name": "Checked",
            "type": "string",
            "values": [
                "No"
            ]
        },
        {
            "id": ".p",
            "name": "",
            "type": "string",
            "values": [
                "AUDUSD"
            ]
        }
    ]
}

Please let me know if you need any additional information.如果您需要任何其他信息,请告诉我。

Something like Apollo GraphGL might be useful for you.像 Apollo GraphGL 这样的东西可能对你有用。 A GraphQL query could pull in the objects in the exact shape/schema you want, so it's more organized for you. GraphQL 查询可以按照您想要的确切形状/模式拉入对象,因此它对您来说更有条理。 Otherwise, you might want to make some kind of parsing utility function to clean up the response object into a new type of object.否则,您可能需要制作某种解析实用程序 function 以将响应 object 清理为新型 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM