简体   繁体   English

使用来自多个 JSON 对象和 arrays 的数据动态渲染 React 组件

[英]Dynamic rendering React components using data from multiple JSON objects and arrays

I am using React Native to dynamically render components.我正在使用 React Native 来动态渲染组件。 Currently the data is being taken by mapping over an array of objects which looks similar to:目前,数据是通过映射一组对象来获取的,这些对象看起来类似于:

 const OBJECTS = [ {name: "Object 1", logo: require('../logos/object1.png')}, {name: "Object 2", logo: require('../logos/object2.png')}, {name: "Object 3", logo: require('../logos/object2.png')}, ]

I then map over these objects to render a card for each object like so:然后我在这些对象上使用 map 为每个 object 渲染一张卡片,如下所示:

 <View> {object.map(item => { return ( <View key={item.key}> <Card> <Text>{item.name}</Text> <Image source={item.logo}/> </Card> </View> ); })} </View>

However now I would like to add additional items to each card which are taking information fetched from an API and retrieved in JSON form.但是现在我想向每张卡添加额外的项目,这些项目正在获取从 API 获取并以 JSON 形式检索的信息。 These JSON objects look similar to the following:这些 JSON 对象类似于以下内容:

Request 1:请求 1:

 fetchPriceActionCreator { "items": [{ "objectId": "object1", "price": 10 }, { "objectId": "object2", "price": 5 }, { "objectId": "object3", "price": 20 } ] }

Request 2:请求 2:

 fetchBalanceActionCreator { "items": [{ "objectId": "object1", "balance": 2, "account": 30022 }, { "objectId": "object2", "balance": 4, "account": 40035 }, { "objectId": "object3", "balance": 6, "account": 50021 } ] }

As the application is growing in complexity, you may notice from the action creators used to make the requests that I have begun to use Redux.随着应用程序变得越来越复杂,您可能会从用于发出请求的操作创建者那里注意到我已经开始使用 Redux。 My challenge is now figuring out a way to associate the retrieved JSON information, with the initial OBJECTS object array so that I can include this information in the relevant dynamically rendered card displaying the name , logo , balance , and price of each object.我现在的挑战是想办法将检索到的 JSON 信息与初始OBJECTS object 数组关联起来,这样我就可以将此信息包含在相关的动态呈现的卡片中,显示每个 ZA68CFDE63131ACB49 的namelogobalanceprice

From what I understand, I need to take the static information from the OBJECTS object array, and move this to initialState in either the reducer or store - however I am completely lost as to how to go about doing this, and how to then associate the two retrieved JSON object arrays, with the OBJECT object array.据我了解,我需要从OBJECTS object 数组中获取 static 信息,并将其移动到减速器或存储中的initialState - 但是我完全不知道如何 Z34D1F91FB2E7 然后如何关联two retrieved JSON object arrays, with the OBJECT object array.

The end result should look something like this:最终结果应如下所示:

 <View> {object.map(item => { return ( <View key={item.key}> <Card> <Text>{item.name}</Text> <Image source={item.logo}/> <Text>{item.price}</Text> <Text>{item.balance}</Text> </Card> </View> ); })} </View>

Any help would be greatly appreciated.任何帮助将不胜感激。

EDIT:编辑:

Currently my redux reducers look like so:目前我的 redux 减速器看起来像这样:

 function priceReducer(priceState = {}, action) { switch (action.type) { case REQUEST_PRICE: return Object.assign({}, priceState, { isFetching: true, didInvalidate: false, }); case RECEIVE_PRICE: return Object.assign({}, priceState, { isFetching: false, didInvalidate: false, lastUpdated: action.lastUpdated, items: action.items, }); default: return Object.assign({}, priceState); } }

in your redux store you can update the state by through merging the initial state and the data coming from the api, like so在您的 redux 商店中,您可以通过合并初始 state 和来自 Z8A385AAZ2ED1267277 的数据来更新 state

const priceState = {
    // some other props
    OBJECTS: [
        { name: "Object 1", logo: require('../logos/object1.png') },
        { name: "Object 2", logo: require('../logos/object2.png') },
        { name: "Object 3", logo: require('../logos/object2.png') },
    ]  // your initial Data as you posted.
}

function priceReducer(priceState = {}, action) {
    switch (action.type)
    {
        case REQUEST_PRICE: {
            return {
                ...priceState,
                didInvalidate: false,
                isFetching: true,
                OBJECTS: priceState.OBJECTS.map((obj) => {
                    const i = action.items.find((item) => item.objectId == obj.name) || {}; // using the conditional operator to avoid runtime error if no item was found for the given object

                    // Example: current object name is "object1", i will the item inside the payload that will have the its objectId equal to "object1"
                    return {
                        ...obj,
                        ...i /// i will be the item inside the payload that has the same object id as the current object name 
                    }

                })

            }
        }

        case RECEIVE_PRICE: {
            return {
                ...priceState,
                didInvalidate: false, 
                lastUpdated: action.lastUpdated,
                OBJECTS: priceState.OBJECTS.map((obj) => {
                    const i = action.items.find((item) => item.objectId == obj.name) || {}; // using the conditional operator to avoid runtime error if no item was found for the given object

                    // Example: current object name is "object1", i will the item inside the payload that will have the its objectId equal to "object1"
                    return {
                        ...obj,
                        ...i /// i will be the item inside the payload that has the same object id as the current object name 
                    }

                })

            }
        }

     default: return priceState;
    }

}

if you are not sure what the ... is its called the spread operator., which is equivalent to Object.assign that you were using如果您不确定...是什么,它称为扩展运算符。相当于您使用的Object.assign

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

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