javascript - 在 ReactJS 中映射数组时,即使数组有元素,浏览器中也不会显示任何内容

标签 javascript reactjs redux

我在 redux 存储中初始化了以下数据

flightSeats: {
    AH001: {
      AH1: [
        {
          seatNumber: "1A",
          isBooked: true
        },
        {
          seatNumber: "1B",
          isBooked: true
        },
        {
          seatNumber: "1C",
          isBooked: false
        },
        {
          seatNumber: "1D",
          isBooked: false
        },
        {
          seatNumber: "1E",
          isBooked: false
        },
        {
          seatNumber: "1F",
          isBooked: false
        }
      ], //.... continues for key AH2 and so on.

在 JSX 中我尝试显示如下

<li>
  <ol className="seats" type="A">

    //the data is printed to console successfully
    {console.log(flightSeats.AH001.AH1)} 

    {this.props.flightSeats.AH001.AH1.map(flightSeat => {
      <li className="seat">
        <input type="checkbox" id={flightSeat.seatNumber} />
        <label htmlFor={flightSeat.seatNumber}>
          {flightSeat.seatNumber}
        </label>
      </li>;
    })}
  </ol>
</li>

我什至在使用调试器时获得了 map 内部的控制。但 map 内没有打印任何内容。请帮忙。

最佳答案

在map方法中,不要使用.map(() =>{})你应该使用.map(() => <Example /> )其中Example是你的jsx。看看我要向您展示的这个例子:

 render() {
      const { products } = this.state;
      const { amount } = this.props;
      return (
        <ProductList>
          {products.map((product) => (
            <li key={product.id}>
              <img
                src={product.image}
                alt={product.title}
              />
              <strong>{product.title}</strong>
              <span>{product.priceFormatted}</span>
              <button
                type="button"
                onClick={() => this.handleAddProduct(product.id)}
              >
                <div>
                  <MdAddShoppingCart size={16} color="#fff" />
                  {' '}
                  {amount[product.id] || 0}
                </div>
                <span>adicionar ao carrinho</span>
              </button>
            </li>
          ))}
        </ProductList>
      );
    }

所有的jsx都在()里面,不是{}

此外,在渲染方法中解构你的 props,以使代码更清晰。

关于javascript - 在 ReactJS 中映射数组时,即使数组有元素,浏览器中也不会显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60556956/

相关文章:

javascript - 通过RequireJS加载本地jQuery、jQuery UI等文件

javascript - 无法验证用户输入 - onkeyup 事件未触发

javascript - .map() 在 render 函数之外工作,但在 render 内部使用时失败

reactjs - 避免使用react-redux重新渲染一个大的项目列表

javascript - AJAX 数据包含一个或多个单词..?

javascript - GWT:将处理程序添加到 native js 事件 onafterprint

javascript - React 中循环对象数组的问题

reactjs - 如何在 React Native Flexbox 中水平包装项目?

javascript - ReactJS 状态没有改变

reactjs - 组件未更新深度嵌套的 redux 对象