问题
I am using Downshift, in order to create a dropdown that displays some menu options. I created a reusable React component with downshift, but now I get this error:
Uncaught TypeError: items.map is not a function
at IconDropdown.react__WEBPACK_IMPORTED_MODULE_2___default.a.createElement.react__WEBPACK_IMPORTED_MODULE_2___default.a.createElement (index.jsx:29)
Here is the code this error refers to:
{isOpen ? (
<div className="dropdown-menu">
{items.map(item => ( // Specifically this map
<button
type="button"
{...getItemProps({ item })}
key={item}
className="dropdown-item"
>
<Icon icon={item.icon} /> {item.name}
</button>
))}
</div>
) : null}
And I am using it in my main Screen Component like this:
items = {
editUser: {
name: 'Edit Info',
icon: 'pencil-square-o'
},
changePassword: {
name: 'Change Password',
icon: 'lock'
},
deleteUsed: {
name: 'Delete user',
icon: 'trash-o'
}
};
render() {
return (
<IconDropdown items={this.items} />
)}
Expected behavior: Render a list of menu items, with their corresponding icons.
Current Behavior: I get the above error. I am not sure what is wrong with this code. On my eyes this is correct.
I can post the entire component if you want, just let me know.
UPDATE, The entire Dropdown Component:
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import Downshift from 'downshift';
import Icon from 'lenses/common/components/Icon';
import './styles.scss';
const IconDropdown = ({ items, ...otherProps }) => (
<Fragment>
<Downshift {...otherProps}>
{({ getItemProps, isOpen, toggleMenu }) => (
<div>
<div className="btn-group">
<button
id="my-select"
type="button"
className="btn btn-primary dropdown-toggle dropdown-toggle-split"
onClick={toggleMenu}
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded={isOpen}
>
<span className="sr-only">Toggle Dropdown</span>
</button>
{isOpen ? (
<div className="dropdown-menu">
{items.map(item => (
<button
type="button"
{...getItemProps({ item })}
key={item}
className="dropdown-item"
>
<Icon icon={item.icon} /> {item.name}
</button>
))}
</div>
) : null}
</div>
</div>
)}
</Downshift>
</Fragment>
);
IconDropdown.propTypes = {
items: PropTypes.shape({
name: PropTypes.string.isRequired,
icon: PropTypes.string
})
};
export default IconDropdown;
回答1:
.map
doesn't exist from an object {}
, it only exists from an array []
. You can:
- Change your object to an array
- Use
Object.keys(items).map(...)
- Use lodash.map
回答2:
Youre iteration on object
this should be array
Just change
items = {
editUser: {
name: 'Edit Info',
icon: 'pencil-square-o'
},
changePassword: {
name: 'Change Password',
icon: 'lock'
},
deleteUsed: {
name: 'Delete user',
icon: 'trash-o'
}
};
TO
items = [{
name: 'Edit Info',
icon: 'pencil-square-o'
},{
name: 'Change Password',
icon: 'lock'
},{
name: 'Delete user',
icon: 'trash-o'
}
];
Or you can use items.enteries.map
to iterate over object
回答3:
items
is not an array. Check post on how to map through object here
An other solution would be to declare items as an array:
items = [
{
name: 'Edit Info',
icon: 'pencil-square-o'
},
{
name: 'Change Password',
icon: 'lock'
},
{
name: 'Delete user',
icon: 'trash-o'
}
];
回答4:
Try to put Object.keys(objectname)
before you array map.
Map function needs an array to work, with Object.keys(objectname)
, you will transform an object {}
to an a valid array []
for map.
Object.keys(items).map(item => ...
来源:https://stackoverflow.com/questions/55137072/react-items-map-is-not-a-function