New to React DnD? Read the overview before jumping into the docs.
First of all, you don't have to use ES6 or ES7 for React DnD. They make some of the code patterns more succinct, but it's up to you whether to enable these transformations.
When I say “ES7”, I mean ES6 + a few extensions that might make it into ES7 (also known as ES2016). What I mark as the ES7 code in the docs is actually ES6 code + class properties + decorators. You can enable these features by putting { "stage": 0 }
into your .babelrc file. You can also enable them individually.
The @
syntax desugars into the simple function calls.
You can see that in every example on the website that uses it, such as:
var DragSource = require('react-dnd').DragSource;
var MyComponent = React.createClass({
/* ... */
});
module.exports = DragSource(type, spec, collect)(MyComponent);
Grab it from the dist folder in the repo.
It exports ReactDnD
(watch the casing!) and ReactDnD.HTML5
for the HTML5
backend.
See the testing tutorial for examples.
Specify the container node as the dragPreview
, but only make the drag handle a dragSource()
.
See the custom drag handle example.
By default, you can't constrain the drag preview movement because the drag preview is drawn by the browser. You can, however, use a custom drag layer where you're free to rendering anything, with any snapping or constraints.
Both DragSource
and DropTarget
let you pass a function as the first parameter instead of a string or a symbol. If you pass a function, it will be given the current props, and it should return a string, a symbol, or (for drop targets only) an array of either.
Because DragSource
and DropTarget
use the partial application, you may compose them using a functional composition helper such as _.flow
. In ES7, you can just stack the decorators to achieve the same effect.
var DragSource = require('react-dnd').DragSource;
var DropTarget = require('react-dnd').DropTarget;
var flow = require('lodash/function/flow');
var YourComponent = React.createClass({
/* ... */
});
module.exports = flow(
DragSource(/* ... */),
DropTarget(/* ... */)
)(YourComponent);
If you are using the HTML5
backend, you can register a drop target for one of the NativeTypes
it exports:
var React = require('react');
var NativeTypes = require('react-dnd/modules/backends/HTML5').NativeTypes;
var DropTarget = require('react-dnd').DropTarget;
var fileTarget = {
drop: function (props, monitor) {
console.log(monitor.getItem().files);
}
};
var FileDropZone = React.createClass({
render() {
var connectDropTarget = this.props.connectDropTarget;
var isOver = this.props.isOver;
var canDrop = this.props.canDrop;
return connectDropTarget(
<div>
{!isOver && !canDrop && 'Drag files from the hard drive'}
{!isOver && canDrop && 'Drag the files here'}
{isOver && 'Drop the files'}
</div>
);
}
});
module.exports = DropTarget(NativeTypes.FILE, fileTarget, function (connect, monitor) {
return {
connectDropTarget: connect.dropTarget(),
isOver: monitor.isOver(),
canDrop: monitor.canDrop()
};
})(FileDropZone);
This is not currently documented, but you can take cues from the built-in HTML5
and Test
backend implementations. The backend contract includes setup()
and teardown()
methods, and connectDragSource()
, connectDragPreview()
and connectDropTarget()
methods that pass the DOM nodes to the backend. Contributions to the documentation are welcome.
React DnD requires React 0.13. Make sure you are using at least that version.
Consider this example:
var Page = React.createClass({
statics: {
willTransitionTo: function (transition, params) {
/* ... */
},
},
render: function () {
/* ... */
}
});
module.exports = DragDropContext(HTML5Backend)(Page);
It might surprise you that your route handler's willTransitionTo
(or a similar method) won't get triggered in this case! React DnD doesn't proxy your components' static methods and properties. This is too fragile and full of edge cases, so you must do it yourself. It's not hard! Just put your statics onto the components returned by React DnD instead:
var Page = React.createClass({
render: function () {
/* ... */
}
});
Page = DragDropContext(HTML5Backend)(Page);
Page.willTransitionTo = function (transition, params) {
/* ... */
};
module.exports = Page;
I know, it's only drag and drop, but I like it.
Stampsy has been using this library, as well as its non-React predecessor that it was based on, since 2013. It's central to the Stampsy user experience, because all the content is created in a drag and drop post editor that uses React DnD.
The next big feature to be added is the support for the touch events backend. This is necessary to make React DnD work on mobile devices. I will be busy in June preparing for my React Europe talk, so expect the touch support to land in July or August.
React DnD was created by Dan Abramov.
Its aim is to expose a powerful API that is browser-agnostic, data-centric, works great with React and Flux, and is testable and extensible. Read The Future of the Drag and Drop API for some context.
It is loosely based on the pre-React code written at Stampsy by Andrew Kuznetsov. Later it received valuable contributions from Alexander Kuznetsov and Nathan Hutchison.
React DnD would not have reached the 1.0 release without the generous donations from:
Gadzhi Kharkharov styled the website, and the fixed-data-table project provided the website template.
Contributing the documentation for the underlying DnD Core library would be a huge help, as it is currently not documented at all, but its concepts leak in some advanced scenarios such as writing tests.
Porting the library to other modern frameworks such as Cycle, Mercury, or Deku, is also appreciated. Such ports would be able to reuse DnD Core logic and the existing backends.
Please let me know via the issue tracker if you create advanced examples such as a Kanban board application, or write a blog post or record a screencast about React DnD, so I can link to it.
While DnD Core is fully tested, React DnD does not currently have any unit tests. Writing them is a great and eagerly desired contribution.
Whether you are a company or an individual, if you enjoy using React DnD, consider donating to my PayPal address: dan.abramov@me.com
. I left my full-time job to work on the open source projects. Your donations help me fund the current and the future development.
⚛