简体   繁体   English

反应 尝试使用forEach函数时显示错误

[英]React. Props error when try to use forEach function

When I try to send the props from parent component Row by condition via forEach iterator I even get the undefined in the child component. 当我尝试通过forEach迭代器按条件从父组件Row发送道具时,我什至在子组件中得到了undefined

I cannot figure out what is the problem is... 我不知道问题是什么...

 // Parent component
 // ... class Row extends...
     // ... some code
      render() {
        const canSelector = {
           selectedItems: []
        }
        return (
            <Row showImage={canSelector.selectedItems.forEach(item => { 
                   item.ID === i ? 'item' : 'error'}) }
            />
          }            
        )


// Child component for test
// ... class XXX extends...
         // ... some code
         const { showImage } = this.props
         console.log(showImage); // gets "undefined", intead of "error" string

Array.prototype.forEach() doesn't return anything, that is the reason that you are getting undefined as the value of showImage in your <Row /> component. Array.prototype.forEach()不返回任何内容,这就是在<Row />组件showImage undefinedshowImage值的showImage

I assume that you are trying to check if the item.ID has the same value as i , and return 'item' or 'error' accordingly? 我假设您正在尝试检查item.ID是否具有与i相同的值,并相应地返回'item''error'

You might want to check Array.prototype.findIndex() instead. 您可能要检查Array.prototype.findIndex()

You can do something like this: 您可以执行以下操作:

<Row showImage={canSelector.selectedItems.findIndex(item => item.ID === i) > -1 ? 'item' : 'error'} />

findIndex() will return the index of the first item in the array that satisfy the condition provided, else it will return -1 . findIndex()将返回满足所提供条件的数组中第一项的索引,否则将返回-1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应何时在道具中使用函数? - React when to use a Function in Props? 优化 React 中的功能。 将 setState 键作为道具? - Optimize the function in React. Having setState key as a props? 让导航栏在 React 中工作。 通过道具向孩子发送function? - Getting a navbar to work in React. Sending a function to a child through props? Eslint + Prettier + React。 道具缩进 - Eslint + Prettier + React. Props indents React 中的 function scope 错误。 无法读取未定义的属性 - function scope error in React. Cannot read property of undefined 如何在 React 中使用 Firebase App Check。 403错误 - How to use Firebase App Check in React. 403 error Windows调整大小错误后的setState与react。 尝试通过动态宽度作为道具 - setState after windows resize error with react. Trying to pass dynamic width as props “RelayObservable”类型上不存在属性“then”<unknown> '。 当我尝试在反应中使用中继获取数据时。 我不知道为什么会出现这个错误</unknown> - Property 'then' does not exist on type 'RelayObservable<unknown>'. when I try to fetch data using relay in react. I don't know why this error come 当我尝试访问它时无法访问反应道具数据。 获取 TypeError 错误 - Can not access react props data when I try to access it. Getting TypeError error 为什么当我尝试使用 props 传递的函数时 preventDefault 停止工作? - Why does preventDefault stop working when I try to use a function passed by props?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM