Image for post
Image for post
The drag and drop component we will build

Create a Drag and Drop Component in React Native

Build Draggable Elements Using the PanResponder API

While brainstorming my next React Native app, here’s my first, I wanted to see if there’s an API that tracks gesture so I could create a drag and drop component.

Luckily, it wasn’t too hard to find: we have the PanResponder! With this we can track gesture as an abstracted state and update our UI accordingly.

What we will be build

Creating a Draggable Component

I found the documentation for PanResponder API is a bit hard to follow, but this example and animation guide helped get me going

After importing PanResponder class, you initiate it with .create method in componentWillMount() life cycle method. You could also write it in the constructor() .

this.panResponder = PanResponder.create() initiates the panResponder and creates a reference. We utilize it in our <Animate.View> component by passing{…this.panResponder.panHandlers} like we’re passing a group of props.

Inside thePanResponder.create() we set onStartShouldSetPanResponder to true so the panResponder will respond to touch feedback. Then we pass an Animated.event to onPanResponderMove: to update the location of our Animated.View circle component that the user is gesturing with.

To get our circle’s position we get the calculated animated value from this.state.pan.getTranslateTransform() and use it to create a transform style that we pass to our Animated.View.

Finally, we adjust delta value so the element won’t jump on the second touch.

At this point we have a draggable circle that the user can interact with:

Returning the Circle to its Initial Location

We want the circle to return to its original location when they release the component. To do this we use onPanResponderRelease to tell the UI how to respond when the user lets go.

Our circle now returns to its initial location:

Creating the Drop Area

Now that we have the drag, we need the drop. We create another component with our drop area and use our Draggable component within it:

Then we add a bit of logic to Draggable, its onPanResponderRelease handler, and add a function to determine if we’re in the drop area. Note: We make assumptions about the drop zone’s location on the screen here.

And that’s it. Our Screen should now look like this:

To see the source code and play with the live demo check out the Expo Snack I created for this demo.

Conclusion

The PanResponder API was a bit intimidating for me at first, but now I think this is a great API that anyone can use to make the user experience in their apps better.

I hope this article has helped you out, reach out in the comments with questions or concerns. Thanks for reading, and happy coding!

*Korean version of this article hasn’t written yet.

React Native Coach

Bringing you the best React Native, GraphQL, Redux, and…

Bi Yoo

Written by

Designer. Programmer. Lover of technology & art. 🦄

React Native Coach

Bringing you the best React Native, GraphQL, Redux, and Mobile Design resources from around the web. To submit stories for review or for inquiries about sponsorship: [email protected].

Bi Yoo

Written by

Designer. Programmer. Lover of technology & art. 🦄

React Native Coach

Bringing you the best React Native, GraphQL, Redux, and Mobile Design resources from around the web. To submit stories for review or for inquiries about sponsorship: [email protected].

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store