简体   繁体   中英

React callback return not iterable instance

I'm using ant-desing(v3.26.14) List component in React. Normally if I try to pass plain array to 'dataSource' property then everything is fine. But when I try to pass array via callback it's given error.

<List dataSource={someArray} /> //is fine
<List dataSource={() => someArray} /> //getting error

Error: 'TypeError: Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a Symbol.iterator method.'

I wonder is this normal behaviour of javascript or some library issue and why that's happening, isn't this the same thing?

I think it's because the List component is expecting the array so it can use the spread, but you are giving it a callback function. It doesn't expect a callback function but an iterator ( array in this case ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM