简体   繁体   English

反应 - 无法在 Object.keys(obj).map 中渲染 html

[英]React - Can't render html in Object.keys(obj).map

I'm building this app with a form in which you input fields as providers, keywords and quantity and hitting "search" it opens a modal with the results of different ajax calls.我正在使用一个表单构建这个应用程序,您可以在其中输入字段作为提供者、关键字和数量,然后点击“搜索”,它会打开一个模式,其中包含不同 ajax 调用的结果。

The flow works fine, but when it comes to show the results into the modal, nothing is displayed.流程工作正常,但是当将结果显示到模式中时,什么都没有显示。 This is the code for my modal.这是我的模态代码。

<Modal
                size="lg"
                show={lgShow}
                //onShow={ (() => loadHtml(html)) }
                //onAfterOpen={ (() => onAfterOpen(html)) }
                onHide={ (() => onHide(lgShow)) }
                aria-labelledby="example-modal-sizes-title-lg"
                className={`fade modal ${client}-results`}
            >
            <Modal.Header closeButton>
            <Modal.Title id="example-modal-sizes-title-lg">
                Your products
            </Modal.Title>
            </Modal.Header>
            <Modal.Body>
                {   
                    Object.keys(ls).map( key => {
                        ls[key] !== null ?
                        switchProvs(key) :
                        ''
                    } )
                }
            </Modal.Body>
            </Modal>

"ls" is an object that collects localStorage values that change every time I search for products of each provider, resulting in something like this: “ls”是一个 object,它收集每次搜索每个提供商的产品时都会更改的 localStorage 值,结果如下:

const ls = {
  provider1: localStorage.getItem('provider1'),
  provider2: localStorage.getItem('provider2'),
  provider3: localStorage.getItem('provider3')
}

This is my switchProvs function:这是我的 switchProvs function:

const switchProvs = ( obj, key ) => {
    switch(key){
        case 'provider1':
            JSON.parse(obj[key]).map( (provider1) => provider1Render(provider1, props) ) 
            break;
        case 'provider2':
            JSON.parse(obj[key]).map( (provider2) => provider2Render(provider2, props) ) 
            break;
        case 'provider3':
            ...
            break;
    }
}

And finally, this is the providernRender function (all perform the same task, even if with different renders):最后,这是 providernRender function(所有执行相同的任务,即使使用不同的渲染):

export const provider2Render = ( provider2, props ) => {
const { attributes: {offers} } = props;

const p = provider2
const id = provider2.someVal

const html = <div className="prod">
                <figure className="prod-image">
                    <small>Provider2</small>
                </figure>
                <div className="prod-content">
                    <strong>{ p.ItemInfo.Title }</strong>
                    <div className="prod-meta" id={id}>
                        <ButtonAdd offerid={id} props={props} clicked={clicked(offers, 'provider2', id)} />
                    </div>
                </div>
            </div>
            
localStorage.removeItem('provider2');

return html
}

The modal body is empty when I perform this actions, but if I add a console.log inside the provider2Render, it is fired.当我执行此操作时,模态主体为空,但如果我在 provider2Render 中添加一个 console.log,它就会被触发。 So the problem is that the functions work (or at least the app enters them when required), but nothing is returned as html.所以问题是这些功能可以工作(或者至少应用程序在需要时输入它们),但没有返回任何内容作为 html。

Object.keys(ls) will return an array of objects based on your data. Object.keys(ls)将根据您的数据返回一个对象数组。 As you are trying to access value from ls using ls[key] will return undefined as it's an array.当您尝试使用ls[key]从 ls 访问值时,将返回 undefined ,因为它是一个数组。 You need to use an index instead.您需要改用索引。

Let's consider the following example让我们考虑以下示例

var ls = {
  provider1: "some object from localstorage",
  provider2: "some object from localstorage",
  provider3: "some object from localstorage"
}

// Object.keys(ls) will return [{some object from localstorage},{some object from localstorage},{some object from localstorage}]

//Now when you map over the Object.keys(ls) you'll get 'key' as an individual element

//To access the object properly you need to get it by index. i.e

Object.keys(ls).map((key,index) => {
       ls[index] !== null ?
       switchProvs(ls[index],ls[index].<provider key name>) :
       ''
} )  

You can read more about你可以阅读更多关于

Object.keys([]): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys Object.keys([]): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Array.map(fn): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map Array.map(fn): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

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

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