我有一个react程序需要其他组件的数据.当父组件发送路径数据时,数组将获取文件夹中的所有文件名,我还需要计算数据长度,以确保文件列表不会为空.一些数据需要存储到状态,并将在下一个进程中重新使用.以下是节目.

  constructor() {
    super()

    this.state = {
      fulldir: null,
      listfileArray: [],
      isDataExist: true
    }
  }
  componentDidUpdate()
  {
    let path = this.props.retrievefilespath
    let dirLocation = Utils.fulldirFunc(path)
    let rdir = fs.readdirSync(dirLocation)
    let countList = 0
    rdir.forEach((filename) => {
        this.state.listfileArray.push({
                id: countList,
                name: filename,
                selected: false
            })
        countList++
    })
    if(countList > 0)
      this.setState({
        isDataExist: true
      })
  }

但是这个程序返回错误

超过最大更新深度时出错.当一个组件

之前我使用vuejs并使用数据来处理数据流

data () {
        return {
            listfile: [],
            isActive: false,
            isDataExist: false,
            arrayIdx: []
        }
    },
    watch: {
        lfdirHandler: function () {
            //reset data from previous action
            Object.assign(this.$data, getInitialData())

            //electron filesystem
            const fs = require('fs')
            if(this.lfdirHandler.length > 0)
            {
                let dirLocation = Utils.fulldirFunc(this.lfdirHandler)
                let rdir = fs.readdirSync(dirLocation)
                var countList = 0
                rdir.forEach((filename) => {
                    this.listfile.push({
                            id: countList,
                            name: filename,
                            selected: false
                        })
                    countList++
                })
                if(countList > 0)
                    this.isDataExist = true
            }
        },

那么,这个问题的最佳解决方案是什么?我的意思是,如何像处理vue那样处理数据流?使用react state正确吗?谢谢

推荐答案

我对这里写的文档感到困惑

componentDidUpdate(prevProps) {
  // Typical usage (don't forget to compare props):
  if (this.props.userID !== prevProps.userID) {
    this.fetchData(this.props.userID);
  }
}

经过几次测试后,我认为应该按照以下代码编写文档:

componentDidUpdate(props) {
  // Typical usage (don't forget to compare props):
  // props.userID as previous data
  if (this.props.userID !== props.userID) {
    this.fetchData(this.props.userID);
  }
}

this prevProps is confusing me.
So, the solution based on the documentation is, you need to compare, whether data inside props.userID is equal to this.props.userID.

笔记:

  • this.props.userID带来新数据
  • props.userID带来旧数据

props.userID正在保存以前操作中的数据.所以,如果之前你将状态userID设置为foo.程序将把它保存到props.userID.

componentDidUpdate(props) {
// this.props.userID  -> foo
// props.userID       -> foo
  if (this.props.userID !== props.userID) {
      // false
      // nothing will run inside here
  }
}
componentDidUpdate(props) {
// this.props.userID  -> foo
// props.userID       -> bar
  if (this.props.userID !== props.userID) {
      // true
      // do something
  }
}

我的节目在这里:

componentDidUpdate()
{
   let path = this.props.retrievefilespath
   //....
}

就是无限地将this.props.retrievefilespath与零相比.这就是程序返回错误的原因.

Reactjs相关问答推荐

根据另一个Select中的选定值更改Select中的值

如何使用皮金贴图在Reactjs中制作 map 上的点之间的线的动画?

Google Firestore无法读取图像-react

useCallback()是否应该用于作为prop传递给子组件的函数,即使该组件包装在memo()中?

如何在物料界面react 的多选菜单中设置最大 Select 数限制

当 URL 与我声明的任何路由都不匹配时,如何使用不会重定向到错误页面的动态 URL? - react 路由 dom

为 Next.js 应用程序目录中的所有页面添加逻辑的全局文件是什么?

画布:用鼠标绘制形状时重新绘制整个画布会导致卡顿

从 redux 调用UPDATE_DATA操作时状态变得未定义

为什么我会收到此错误:无法在我的react 包上读取 null 的属性(读取useState)?

我正在try 将一组加载程序函数发送到我的路由,它抛出一个错误 Handler is not a funtion in a reactJs application

导入样式化组件时未导入组件内容

在 React 中将路径与查询变量匹配

如何在对话素材ui中设置边框半径?

React 中的 Slide API Mui 在开始而不是结束时触发侦听器

如何解决在 react.js 中找不到模块错误

React Router v6 - 访问嵌套路由和处理手写 url 的正确方法

如何根据数据库中的值设置单选按钮选中? - react

设置 Material UI 表格默认排序

gtag:为什么谷歌分析即使没有被授权也会收集数据