React beautiful dnd 中的简单列表不起作用

Saa*_*aad 3 reactjs react-dnd react-beautiful-dnd

我一直在关注 Egghead 的官方课程(https://egghead.io/lessons/react-reorder-a-list-with-react-beautiful-dnd和示例工作代码: https: //codesandbox.io/s /52p60qqlpp)并写道:

https://codesandbox.io/s/00k3rwq3qn

然而,它实际上并没有拖放。我查看了几个示例,但无法在代码中发现问题。我真的很感谢有人对我的错误提出反馈。

谢谢

Ian*_*ser 9

Draggable我认为使用内联样式在in中效果不佳react-beautiful-dnd。如果您想将样式应用到Task您应该使用的组件styled-components,此示例显示如果您删除内联样式配置https://codesandbox.io/s/6lq0854m6r ,它就可以工作。

而是使用样式组件

import React from "react";
import styled from "styled-components";
import { Draggable } from "react-beautiful-dnd";

const Container = styled.div`
  margin: 10px;
  border: 1px solid black;
`;

export default class Task extends React.Component {
  render() {
    return (
      <Draggable draggableId={this.props.task.id} index={this.props.index}>
        {(provided, snapshot) => (
          <Container
            {...provided.draggableProps}
            {...provided.dragHandleProps}
            ref={provided.innerRef}
          >
            {this.props.task.content}
          </Container>
        )}
      </Draggable>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑: 似乎您可以将内联样式应用于可拖动,但是您应该DraggableProps.styles在组件的子函数内扩展Draggable,请参阅此处

{(provided, snapshot) => (
    const style = {
        margin: "10px",
        border: "1px solid black",
        ...provided.draggableProps.style,
    };
    return <div
        {...provided.draggableProps}
        {...provided.dragHandleProps}
        ref={provided.innerRef}
        style={style}
        >
        {this.props.task.content}
    </div>
)}
Run Code Online (Sandbox Code Playgroud)