【问题标题】:Map local .json data in React在 React 中映射本地 .json 数据
【发布时间】:2018-04-30 11:58:58
【问题描述】:

我有一个格式如下的本地 .json 文件

    {
  "results": [
    {
      "id": 1,
      "title": "2 bedroom apartment to rent",
      "location": "30 South Colonnade London E14 5EZ",
      "description": "The building offers a communal lifestyle which consists of a large lounge area with dining room, working space, TV lounge, gym, stylish meeting rooms, free table tennis, free WIFI and a spacious communal roof terrace. Local transport includes Canning Town DLR and Jubilee Line (0.1 miles). Argo Apartments is managed by Grainger plc, one of the UK's leading professional landlords with over 100 years experience.",
      "price": "1,800",
      "beds": 2,
      "bathrooms": 1,
      "landlord": "Hamptons International Lettings",
      "images": ["image1", "image2"]
    },
    {
      "id": 2,
      "title": "2 bedroom flat to rent",
      "location": "Textile Building, Chatham Place, Hackney, London, E9",
      "description": "SHORT LET - Forming part of the new eagerly awaited Textile Building in Hackney Central E8, this stunning two double bedroom property has been finished to the highest standard, featuring two modern fitted bathrooms (one en-suite), two equal double bedrooms, spacious open plan reception with brand new integrated kitchen, exposed brickwork and communal underground bike storage.",
      "price": "2,400",
      "beds": 2,
      "bathrooms": 1,
      "landlord": "Stirling Ackroyd, Hackney",
      "images": ["image1", "image2"]
    },
    {
      "id": 3,
      "title": "3 bedroom apartment to rent",
      "location": "Sixth Floor Tower Building 11 York Road London SE1 7NX",
      "description": "A fantastic three bedroom apartment set in this popular development close to Vauxhall and Stockwell tube stations. The apartment is neutrally decorated throughout and is available either furnished or unfurnished. Residents will enjoy the plethora of shops, bars and restaurants and leisure activities in this popular part of London as well as the excellent commuter links towards Central London.",
      "price": "2,050",
      "beds": 3,
      "bathrooms": 2,
      "landlord": "Regent Letting and Property Management",
      "images": ["image1", "image2"]
    }
  ],
  "newResults" : [
      {
      "id": 4,
      "title": "2 bedroom flat to rent",
      "location": "Conway Street, Fitzrovia, W1T",
      "description": "Complete is delighted to market this newly renovated period property situated on the corner of Fitzroy Square in the heart of Fitzrovia, W1. The property is located on the ground floor and comes with 2 double bedrooms, shower room, open plan living and kitchen area. The kitchen is fully fitted with Siemens appliances and Duravit fittings in the bathroom. This apartment has high ceiling and retains its original period features. Located on the corner of this garden square this development was built in the 18th Century and benefits from being in the heart of vibrant London whilst also being a quiet residential area.",
      "price": "1,500",
      "beds": 2,
      "bathrooms": 1,
      "landlord": "Barnard Marcus Lettings",
      "images": ["image1", "image2"]
    }  
  ]
}

我希望能够调用映射到组件的相关信息如下

class ResultsLists extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      results: resultsData
    }
  }

  render() {
    const results = this.state.results.results.map((result) =>
      <li>{result}</li>
    );

    return (
      <div className="rst-List">
        <ul className="rst-List_Items">
          {results}
        </ul>
      </div>
    )
  }
}

我似乎无法让它太工作并且收到错误 - 对象作为 React 子对象无效。关于为什么这不起作用,似乎有各种各样的变化,想知道是否有人可以进行健全性检查。

【问题讨论】:

  • 当你 console.log 结果你得到什么?

标签: javascript arrays json reactjs


【解决方案1】:

您正在映射 results,但随后您将整个 result 对象放入 JSX。

这是一个如何映射数据的示例:

const someData = [{ name: "Colin" }, { name: "Ricardo" }];

const App = () => {
  const temp = someData.map(o => <li>{o.name}</li>);

  return (
    <div>
      <ul>{temp}</ul>
    </div>
  );
};

请注意,我们有{o.name} 而不仅仅是{o}。

工作示例here。

所以在你的情况下,你需要这样的东西:

const results = this.state.results.results.map((result) =>
  <li>{result.title}</li>
);

【讨论】:

  • 因此您不能只吐出所有数据,它需要显示该集合的特定部分。明白了,谢谢
  • 正确。您需要为所有数据添加多个&lt;li&gt;。如果有帮助,请点赞/标记为正确:)
猜你喜欢
  • 2021-03-25
  • 2020-08-21
  • 2020-03-26
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 2023-01-02
  • 2020-10-28
  • 2021-01-16
相关资源
最近更新 更多