【问题标题】:React TypeError: Cannot convert undefined or null to objectReact TypeError:无法将未定义或空值转换为对象
【发布时间】:2020-08-04 03:08:56
【问题描述】:

我正在我的 react 应用程序中创建一个上一个和下一个按钮。我正在尝试获取页面的当前索引(我有一个存储在对象中的页面列表)

这是代码

import React, { useState, useEffect } from "react";

function Preview({ match }) {
  const [imageName, setImageName] = useState(); //IMPORTANT
  const [images, setImages] = useState(); //IMPORTANT

  const [currentIndex, setCurrentIndex] = useState(); //IMPORTANT

  function getIndexByImageName(name) { //IMPORTANT
    Object.keys(images).map((image, index) => {
      if (image === name) return index;
    });
  }

  function importAll(r) {
    let images_ = {};

    r.keys().map((item, index) => {
      images_[item.replace("./", "")] = r(item);
    });

    return images_;
  }

  useEffect(() => {
    //This sets the state.
    setImageName(String(match.params.id));
    setImages(importAll(require.context("../coco-images", false, /\.jpg/)));
  }, []);

  useEffect(() => {
    setCurrentIndex(getIndexByImageName(imageName)); //IMPORTANT (This sets the current index. But returns an error)
  }, []);

  return null;
}

export default Preview;

我得到的错误是“TypeError: Cannot convert undefined or null to object”

为了清楚起见,imageName 和 images 不为 null 或未定义(因为当我 console.log 时,它有我需要的数据)。

提前致谢!

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    我已经解决了。

    我通过创建状态“finishedLoading”来修复它,并在finishedLoading 为真时设置状态

    这里是完整的代码

    import React, { useState, useEffect } from "react";
    import loading from "../images/loading.gif";
    
    function Preview({ match }) {
      const [imageName, setImageName] = useState();
      const [images, setImages] = useState();
      const [finishedLoading, setFinishedLoading] = useState(false);
      const [currentIndex, setCurrentIndex] = useState();
    
      function setCurrent() {
        if (finishedLoading) {
          Object.keys(images).map((image, index) => {
            if (image === imageName) setCurrentIndex(index);
          });
        } else {
          return null;
        }
      }
    
      function importAll(r) {
        let images_ = {};
    
        r.keys().map((item, index) => {
          images_[item.replace("./", "")] = r(item);
        });
    
        setFinishedLoading(true);
        return images_;
      }
    
      useEffect(() => {
        setImageName(String(match.params.id));
        setImages(importAll(require.context("../coco-images", false, /\.jpg/)));
      }, []);
    
      useEffect(() => {
        if (finishedLoading) setCurrent(); //I've change getIndexByName to setCurrent
      }, [finishedLoading]);
    
      return null;
    }
    
    export default Preview;
    

    【讨论】:

      【解决方案2】:

      如何为图像分配默认值:

      const [images, setImages] = useState({}); //IMPORTANT
      

      【讨论】:

      • 我不能,因为我是从文件夹中导入的。
      • 为什么不能像上面那样在useState 中分配{}?它只是一个默认值,当您导入图像时,它将被覆盖
      • 嗯。好的,我试试
      • 还是没有运气。我会尝试寻找其他解决方案
      猜你喜欢
      • 2021-01-20
      • 2020-03-21
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 2021-01-23
      • 1970-01-01
      相关资源
      最近更新 更多