我是新来的人,我想用react-dnd来实现拖放。但是我不能在基于类的组件中使用钩子。由于代码的以下部分,我收到了错误消息。
const [{ isDragging }, drag] = useDrag({
item: { type: ItemTypes.Student },
collect: (monitor) => ({
isDragging: !!monitor.isDragging(),
}),
});
如何在仍然使用基于类的组件的同时实现相同的功能? 下面是完整的代码
class App extends Component {
state = {
values: [],
};
const [{ isDragging }, drag] = useDrag({
item: { type: ItemTypes.Student },
collect: (monitor) => ({
isDragging: !!monitor.isDragging(),
}),
});
divStyle = {
"margin-left": "20px",
};
componentDidMount() {
axios.get("http://localhost:5000/api/student").then((response) => {
this.setState({
values: response.data,
});
});
}
render() {
return (
<div
ref={drag}
style={{
opacity: isDragging ? 0.5 : 1,
fontSize: 25,
fontWeight: "bold",
cursor: "move",
}}
>
<Header as="h2">
<Icon name="users" />
<Header.Content>Reactivities</Header.Content>
</Header>
{this.state.values.map((student: any) => (
<DndProvider backend={Backend}>
<List key={student.id} relaxed>
<List.Item>
<Image
style={this.divStyle}
avatar
src="https://react.semantic-ui.com/images/avatar/small/daniel.jpg"
/>
<List.Content>
<List.Header as="a">
{student.firstName} {student.lastName}
</List.Header>
<List.Description></List.Description>
</List.Content>
</List.Item>
</List>
</DndProvider>
// <List.Item key={value.id}>{value.name}</List.Item>
))}
</div>
);
}
}
export default App;