Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextjs with @dnd-kit #1586

Open
RSelwa opened this issue Jan 9, 2025 · 0 comments
Open

Nextjs with @dnd-kit #1586

RSelwa opened this issue Jan 9, 2025 · 0 comments

Comments

@RSelwa
Copy link

RSelwa commented Jan 9, 2025

Hi there,
we got a similar problem than #1436 (comment), even with the version 0.0.5 and Next 14/15. The solution to set reactStrictMode to false works fine but for production mode, set reactStrictMode to false is not good.

Below are the examples used with Next (not working) and Vite (working).

import React, { useState } from 'react';
import { ControlledExample } from './drag';

export function App(props) {
  const [items, setItems] = useState([0]);

  function insert() {
    setItems([...items, items.length]);
    console.log(items);
  }

  return (
    <div className='App'>
      <button onClick={insert}>OK</button>
      <ControlledExample it={items} />
    </div>
  );
}
import { DragDropProvider } from '@dnd-kit/react';
import { useSortable } from '@dnd-kit/react/sortable';
import { move } from '@dnd-kit/helpers';

const styles = { display: 'inline-flex', flexDirection: 'row', gap: 20 };

export function ControlledExample({ it }) {
  const [items, setItems] = useState([it]);

if(!window) return 

useEffect( () => {
  setItems(it)
}, [it])

  return (
    <DragDropProvider
      onDragEnd={event => {
        setItems(items =>
          move(items, event.operation.source, event.operation.target)
        );
      }}
    >
      <div style={styles}>
        {items.map((id, index) => (
          <Sortable key={id} id={id} index={index} />
        ))}
      </div>
    </DragDropProvider>
  );
}

function Sortable({ id, index }) {
  const { ref } = useSortable({ id, index });

  return <button ref={ref}>Item {id}</button>;
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant