简体   繁体   English

对象作为 react child -react 无效

[英]Objects are not valid as react child -react

I have a object inside a variable called clickedCountry that has the structure {name:x, population: x, region:x, capital: x...} .我在一个名为clickedCountry的变量中有一个对象,其结构为{name:x, population: x, region:x, capital: x...} I can access it via clickedCountry.population .我可以通过clickedCountry.population访问它。 However, if I want to access it programmatically (where I wrote, works not) I get但是,如果我想以编程方式访问它(我写的地方,不起作用)我得到

Objects are not valid as React children对象作为 React 子对象无效

 { ['Name', 'Population', 'Region','Capital', 'Currencies', 'Languages'].map(el => { let name = el.toLowerCase() console.log(typeof(name)) //=>returns string console.log(name) //=> returns population, region etc. return ( <Typography gutterBottom component="div"> <span className='font-600'>{el}:</span> {clickedCountry.name} //=> works {clickedCountry[name]} //=> works not {clickedCountry[`${name}`]} //=>works not {clickedCountry[`${el.toLowerCase()}`]} //=>works not </Typography> ) }); }

You are returning valid JSX for every element in your array.您正在为数组中的每个元素返回有效的 JSX。 You also need to return the mapped array.您还需要返回映射的数组。

You might want to do this你可能想这样做

{
return ['Name', 'Population', 'Region','Capital', 'Currencies', 'Languages'].map(el => {

    let name = el.toLowerCase()
    console.log(typeof(name)) //=>returns string
    console.log(name) //=> returns population, region etc.

    return (
      <Typography gutterBottom component="div">
        <span className='font-600'>{el}:</span> 
         {clickedCountry.name} //=> works
         {clickedCountry[name]}  //=> works not 
         {clickedCountry[`${name}`]} //=>works not
         {clickedCountry[`${el.toLowerCase()}`]} //=>works not 
      </Typography>
    )

  });

}

It sounds like you have an array of objects that don't necessarily have the same keys, but you want to iterate over them anyway using the keys they do have to create some JSX.听起来您有一个对象数组,它们不一定具有相同的键,但是您无论如何都想使用它们必须创建一些 JSX 的键来迭代它们。

Here we're passing in the data array as a component prop.在这里,我们将数据数组作为组件道具传递。 We map over the array and for each object we get its keys ( Object.keys() returns an array of its own).我们map数组并为每个对象获取它的键( Object.keys()返回它自己的数组)。 We then map over the keys array and for each object property with that key we return some JSX.然后我们map到键数组,并为每个具有该键的对象属性返回一些 JSX。

 const { useState } = React; function Example({ data }) { return ( <div> {data.map(obj => { const keys = Object.keys(obj); return ( <div className="country"> {keys.map(key => { return ( <div> <span className="label">{key}: </span> {obj[key]} </div> ); })} </div> ); })} </div> ); } const data = [ { name: 'US', population: '320M', region: 'Georgia', continent: 'North America' }, { name: 'Spain', population: '47M', capital: 'Madrid', anthem: 'Marcha Real', continent: 'Europe' } ]; ReactDOM.render( <Example data={data} />, document.getElementById('react') );
 .country { background-color: #efefef; padding: 0.2em; border: 1px solid #787878; } .country:not(:last-child) { margin-bottom: 1em; } .label { text-transform: capitalize; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> <div id="react"></div>

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

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