首页 > 解决方案 > 从API获取数据时如何解决React js中的未定义错误?

问题描述

收到类似 TypeError 着陆的错误。插槽未定义

<div className="notification" />
        </header>
        <main
          style={
            ready
              ? {}
              : {
                  height: "100vh",
                  overflow: "hidden"
                }
          }>
          <LandingLoader className={ready ? "fade-out" : ""} />
          <ul
            className="slots"
            style={{
              visibility: ready ? "visible" : "hidden"
            }}>
            {landing &&
              landing.Slots.map((slot,index) => {
                if (slot && !slot.removed) {
                  let fullWidth = "100%";
                  let maxWidth = 576;

                  let style = {};
                  style["width"] = fullWidth;
                  style["maxWidth"] = fullWidth;
                  style["height"] = slot.HeightPixel + "px";

                  return (
                    <li key={index} className="slots" style={style}>
                      <Slot slotItem={slot} slotIndex={index} />
                    </li>
                  );
                }
              })}
          </ul>
        </main>
      </div>

标签: jsonreactjsapi

解决方案


您可以为插槽添加另一个检查,例如

landing && landing.Slots && landing.Slots.map

还请检查,是Slots还是slots。可能是拼写错误


推荐阅读