最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react-beautiful-dnd拖拽排序功能的實現過程

 更新時間:2024年07月08日 09:20:59   作者:qing_小諾  
這篇文章主要介紹了react-beautiful-dnd拖拽排序功能的實現過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

如果 react 項目中需要用到拖拽功能,可以使用 react-beautiful-dnd 插件。

1、react-beautiful-dnd插件

github官網鏈接:https://github.com/atlassian/react-beautiful-dnd/blob/master/docs/about/examples.md

打開后顯示下圖:

2、查看所有范例

點上圖中的“All the examples!”,可以查看所有范例(鏈接:https://react-beautiful-dnd.netlify.app/

如下圖:

3、代碼示例

點擊上圖中的“Simple horizontal list”

可以看到代碼示例:

index.js代碼如下:稍加改造就能直接用到項目中啦~~~

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
 
// fake data generator
const getItems = count =>
  Array.from({ length: count }, (v, k) => k).map(k => ({
    id: `item-${k}`,
    content: `item ${k}`,
  }));
 
// a little function to help us with reordering the result
const reorder = (list, startIndex, endIndex) => {
  const result = Array.from(list);
  const [removed] = result.splice(startIndex, 1);
  result.splice(endIndex, 0, removed);
 
  return result;
};
 
const grid = 8;
 
const getItemStyle = (isDragging, draggableStyle) => ({
  // some basic styles to make the items look a bit nicer
  userSelect: 'none',
  padding: grid * 2,
  margin: `0 ${grid}px 0 0`,
 
  // change background colour if dragging
  background: isDragging ? 'lightgreen' : 'grey',
 
  // styles we need to apply on draggables
  ...draggableStyle,
});
 
const getListStyle = isDraggingOver => ({
  background: isDraggingOver ? 'lightblue' : 'lightgrey',
  display: 'flex',
  padding: grid,
  overflow: 'auto',
});
 
class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: getItems(6),
    };
    this.onDragEnd = this.onDragEnd.bind(this);
  }
 
  onDragEnd(result) {
    // dropped outside the list
    if (!result.destination) {
      return;
    }
 
    const items = reorder(
      this.state.items,
      result.source.index,
      result.destination.index
    );
 
    this.setState({
      items,
    });
  }
 
  // Normally you would want to split things out into separate components.
  // But in this example everything is just done in one place for simplicity
  render() {
    return (
      <DragDropContext onDragEnd={this.onDragEnd}>
        <Droppable droppableId="droppable" direction="horizontal">
          {(provided, snapshot) => (
            <div
              ref={provided.innerRef}
              style={getListStyle(snapshot.isDraggingOver)}
              {...provided.droppableProps}
            >
              {this.state.items.map((item, index) => (
                <Draggable key={item.id} draggableId={item.id} index={index}>
                  {(provided, snapshot) => (
                    <div
                      ref={provided.innerRef}
                      {...provided.draggableProps}
                      {...provided.dragHandleProps}
                      style={getItemStyle(
                        snapshot.isDragging,
                        provided.draggableProps.style
                      )}
                    >
                      {item.content}
                    </div>
                  )}
                </Draggable>
              ))}
              {provided.placeholder}
            </div>
          )}
        </Droppable>
      </DragDropContext>
    );
  }
}
 
// Put the thing into the DOM!
ReactDOM.render(<App />, document.getElementById('root'));

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 代碼解析React中setState同步和異步問題

    代碼解析React中setState同步和異步問題

    前端框架從MVC過渡到MVVM。從DOM操作到數據驅動,一直在不斷的進步著,本文給大家介紹React中setState同步和異步問題,感興趣的朋友一起看看吧
    2021-06-06
  • React服務端渲染和同構的實現

    React服務端渲染和同構的實現

    本文主要介紹了React服務端渲染和同構的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • 解決React報錯`value` prop on `input` should not be null

    解決React報錯`value` prop on `input` should&

    這篇文章主要為大家介紹了React報錯`value` prop on `input` should not be null解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 從零開始搭建webpack+react開發(fā)環(huán)境的詳細步驟

    從零開始搭建webpack+react開發(fā)環(huán)境的詳細步驟

    這篇文章主要介紹了從零開始搭建webpack+react開發(fā)環(huán)境的詳細步驟,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • React class和function的區(qū)別小結

    React class和function的區(qū)別小結

    Class組件和Function組件是React中創(chuàng)建組件的兩種主要方式,本文主要介紹了React class和function的區(qū)別小結,具有一定的參考價值,感興趣的可以了解一下
    2023-10-10
  • React內存泄漏的常見原因及避免策略

    React內存泄漏的常見原因及避免策略

    內存泄漏是指程序中分配的內存未能正確釋放,導致內存占用不斷增加,最終可能影響應用性能甚至崩潰,在React中,內存泄漏常發(fā)生于組件卸載后,本文將詳細介紹內存泄漏在React中的常見原因及避免策略,需要的朋友可以參考下
    2025-03-03
  • 淺談react受控組件與非受控組件(小結)

    淺談react受控組件與非受控組件(小結)

    本篇文章主要介紹了淺談react受控組件與非受控組件(小結),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • react基于Ant Desgin Upload實現導入導出

    react基于Ant Desgin Upload實現導入導出

    本文主要介紹了react基于Ant Desgin Upload實現導入導出,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-01-01
  • React實現Step組件的示例代碼

    React實現Step組件的示例代碼

    這篇文章主要為大家詳細介紹了React實現Step組件(步驟條組件)的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-01-01
  • react-json-editor-ajrm解析錯誤與解決方案

    react-json-editor-ajrm解析錯誤與解決方案

    由于歷史原因,項目中 JSON 編輯器使用的是 react-json-editor-ajrm,近期遇到一個嚴重的展示錯誤,傳入編輯器的數據與展示的不一致,這是產品和用戶不可接受的,本文給大家介紹了react-json-editor-ajrm解析錯誤與解決方案,需要的朋友可以參考下
    2024-06-06

最新評論

无棣县| 锡林郭勒盟| 泸溪县| 荥经县| 常熟市| 西丰县| 观塘区| 上饶县| 合肥市| 太和县| 腾冲县| 丰宁| 伊吾县| 怀集县| 福贡县| 屯昌县| 鸡西市| 通榆县| 清丰县| 平塘县| 宣城市| 新源县| 若尔盖县| 页游| 玉山县| 东海县| 阳山县| 柞水县| 南川市| 务川| 高密市| 全州县| 旌德县| 恩平市| 高陵县| 阿鲁科尔沁旗| 新乐市| 育儿| 雅安市| 邹平县| 元江|