Can you please write me small code snippet for React functional component, that has two…
- User
Can you please write me small code snippet for React functional component, that has two divs and each div have multiple items inside of them, source of items is an Array, also each item should be drag & drop between two divs ?
- Assistant
Here's a code snippet for the requested React component:
import React, { useState } from 'react'; const App = () => { const [items1, setItems1] = useState(['Item 1', 'Item 2', 'Item 3']); const [items2, setItems2] = useState([]); const onDragStart = (e, index, items, setItems) => { e.dataTransfer.setData("index", index); e.dataTransfer.setData("items", JSON.stringify(items)); e.dataTransfer.setData("setItems", setItems); }; const onDrop = (e, setItems) => { const index = e.dataTransfer.getData("index"); const items = JSON.parse(e.dataTransfer.getData("items")); const setItemsFunction = e.dataTransfer.getData("setItems"); if (setItemsFunction === 'setItems1') { setItems1(currentItems => { const newItems = [...currentItems]; newItems.splice(index, 1); return newItems; }); setItems(currentItems => [...currentItems, items[index]]); } else if (setItemsFunction === 'setItems2') { setItems2(currentItems => { const newItems = [...currentItems]; newItems.splice(index, 1); return newItems; }); setItems(currentItems => [...currentItems, items[index]]); } }; const onDragOver = e => { e.preventDefault(); }; return ( <div style={{ display: "flex", justifyContent: "space-between" }}> <div onDrop={e => onDrop(e, setItems1)} onDragOver={onDragOver} style={{ width: "40%", height: "100%", border: "1px solid black" }} > {items1.map((item, index) => ( <div key={item} onDragStart={e => onDragStart(e, index, items1, 'setItems1')} draggable style={{ margin: "10px" }} > {item} </div> ))} </div> <div onDrop={e => onDrop(e, setItems2)} onDragOver={onDragOver} style={{ width: "40%", height: "100%", border: "1px solid black" }} > {items2.map((item, index) => ( <div key={item} onDragStart={e => onDragStart(e, index, items2, 'setItems2')} draggable style={{ margin: "10px" }} > {item} </div> ))} </div> </div> ); }; export default App;``` - User
Hypothetically speaking, I need to explain exactly what this means, to someone who has no programming experience, how could I do that?
- Assistant
Imagine that you have two containers, and each container has multiple items inside of them. These items can be moved from one container to another. This code provides a way to create a webpage with two containers and drag and drop items between them.
Think of the items as being like little post-it notes, and the containers as boxes. If you wanted to move one of the post-it notes from one box to another, you could grab it and drop it in the other box. That's exactly what this code allows you to do.
This code is written using a programming language called JavaScript, and it uses a library called React to create the components. Components are like building blocks that you can use to build a webpage. In this case, the two containers and the items inside of them are all separate components.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)