嗨,我是 React 的新手,所以请耐心等待。有人能告诉我为什么我打了一个无效的挂机电话吗?我认为问题与 useState(null) 或它正在使用的函数有关。下面的代码显示了我正在尝试在任何帮助中使用它的全部能力,将不胜感激。
import React, { useState } from 'react';
import ScrollMenu from 'react-horizontal-scrolling-menu';
import './horizontalscroll.css';
const data = [
{
category: "Brands",
items: ["Gucci", "Calvin klein", "Prada"]
},
{
category: "Films",
items: ["Inception ", "Dark Night", "Home Alone"]
}
];
const [category, setCategory] = useState(null);
const onClick = category => {
setCategory(category);
};
// One item component
// selected prop will be passed
const MenuItem = ({ text, selected }) => {
return (
<div
className="menu-item"
>
{text}
</div>
);
};
// All items component
// Important! add unique key
export const Menu = (list) => list.map(el => {
const { category } = el;
return (
<MenuItem
text={category}
key={category}
/>
);
});
const Arrow = ({ text, className }) => {
return (
<div
className={className}
>{text}</div>
);
};
const ArrowLeft = Arrow({ text: '<', className: 'arrow-prev' });
const ArrowRight = Arrow({ text: '>', className: 'arrow-next' });
class HorizantScroller extends React.Component {
state = {
selected: 0
};
onSelect = key => {
this.setState({ selected: key });
}
render() {
const { selected } = this.state;
// Create menu from items
const menu = Menu(list, selected);
return (
<div className="HorizantScroller">
<ScrollMenu
data={menu}
arrowLeft={ArrowLeft}
arrowRight={ArrowRight}
selected={selected}
onSelect={this.onSelect}
onClick={onClick}
/>
</div>
);
}
}
export default HorizantScroller;