question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to make 3.0.0-alpha.7 work with react 16.9

See original GitHub issue

I have the following code:

const MyComponent = props =>
  <div>
    <Reorder
      reorderId="research"
      className={"reorder"}
      lock="horizontal"
      holdTime={500}
      onReorder={() => {console.log("reorder")}}
      autoScroll={true}
      disabled={false}
      disableContextMenus={true}>
        {["1","2","3","4"].map(v => <Cell key={v} name={v} />)}
    </Reorder>
  </div>

const Cell = props =>
  <div style={{
    backgroundColor: "lightpink", 
    border: "1px solid green", 
    padding: 20, 
    userSelect: "none"}}>{props.name}</div>

It doesn’t seem to work and I feel stupid for asking but am I missing something?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DoubleHelixXcommented, May 25, 2021

Hooks Implementation: @JakeSidSmith @tjimsk

  1. Use useCallback for the same functionality you would do when binding this to a function within a Class component.
  2. Instead of the helper function I instead used something I was familiar with for reordering arrays.
  • Install npm install array.prototype.move --save
  • Import “array.prototype.move” so you can use the .move functionality. More about this here.
  1. You would have useState declared or useSelector if you are using Redux. For this example I am using useState for now. const [photos, setPhotos] = useState([])

  2. You would then set your state within the useCallback hook expanding upon bullet point number 1. In this example I have the set state reference declared as setPhotos.

const OnReorder = useCallback((event, previousIndex, nextIndex, fromId, toId) => {
 console.log("onReorder:", event, previousIndex, nextIndex);
     if ( CONDITIONAL_STATEMENT)
        setPhotos([...photos.move(previousIndex, nextIndex)]);
     },
     [photos] // Tells React to memoize regardless of arguments
);

For the Reorder component, I pass in the hook reference: onReorder={OnReorder}.

<Reorder
    reorderId="research"
    className={"reorder"}
    //   lock="horizontal"
    holdTime={1000}
    component="span"
    mouseHoldTime={100}
    onReorder={OnReorder}
    autoScroll={true}
    disabled={false}
    disableContextMenus={true}
    placeholder={
      <S.Placeholder /> // Custom placeholder element (optional), defaults to clone of dragged element
    }
>

I’m creating a Tinder Clone and wanted to have something similar to their drag and drop system.

I managed to replicate 90% of the same look.

Tinder_Reorder_Test

Side Note

I was searching for a drag and drop reorder component and stumbled upon two. However, I haven’t played around with the second option and I believe it doesn’t have callback functionality but I could be wrong. Leaving this here in case it could help others:

  • A second option was created by VaishakVk called react-drag-reorder.
  • Also for those wanting to see the full code I use for the Tinder Clone I am working on, I will update this post soon when I finish the project. [Github Link Coming Soon]
  • Lastly, I would love the functionality where I can have access to the hover position index before dropping. This way I can update my state on its hover position rather than its drop position @JakeSidSmith
0reactions
JakeSidSmithcommented, Sep 21, 2021

Hey, @tmm1 could you please open a pull request, or new issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Could not resolve dependency" error with "react@^16.0" #9426
Current Behavior Installing @react-navigation/stack fails due to dependency error. How to reproduce I've just created a new react-native ...
Read more >
Could not resolve dependency error peer react@"^16.8.0
While trying to install npm install , I am getting below error, could someone please advise, what is the best approach to resolve...
Read more >
unable to resolve module react-native-elements - You.com
This error means that either you haven't installed the react-navigation module or that you have installed the module but didn't re-built your project...
Read more >
React v16.9.0 and the Roadmap Update
Today we are releasing React 16.9. It contains several new features, bugfixes, and new deprecation warnings to help prepare for a future ...
Read more >
Material UI 14 dependency issue - JSON Forms community
I want to use JSONForms in my react typescript project that uses react v17, material ui v5. I setup a separate “trial project”...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found