{"id":329567,"date":"2022-02-14T15:02:59","date_gmt":"2022-02-14T15:02:59","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=329567"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=329567","title":{"rendered":"<span>React Drag &amp; Drop: \u00ab\u0418\u0433\u0440\u0430 \u0432 \u0431\u0443\u0442\u044b\u043b\u043a\u0438\u00bb<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"705\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/367\/3f6\/e19\/3673f6e192fe676c4154628523421027.png\" data-width=\"1228\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442!<\/p>\n<p>\u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0421\u0435\u0440\u0433\u0435\u0439, \u044f \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043e\u0442\u0434\u0435\u043b\u0430 \u0441\u043f\u0435\u0446\u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 <a href=\"https:\/\/kts.studio\">KTS<\/a>. \u041d\u0430\u0448 \u043e\u0442\u0434\u0435\u043b \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043f\u0440\u043e\u043c\u043e\u043a\u0430\u043c\u043f\u0430\u043d\u0438\u0439.\u00a0<\/p>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u043a\u0430\u043a \u0432 1-\u0439 \u043a\u043d\u0438\u0433\u0435 \u043e \u0413\u0430\u0440\u0440\u0438 \u041f\u043e\u0442\u0442\u0435\u0440\u0435 \u0413\u0435\u0440\u043c\u0438\u043e\u043d\u0430 \u0440\u0430\u0437\u0433\u0430\u0434\u044b\u0432\u0430\u043b\u0430 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0437\u0430\u0433\u0430\u0434\u043a\u0443  \u0441 \u0431\u0443\u0442\u044b\u043b\u043e\u0447\u043a\u0430\u043c\u0438 \u0432\u043e\u043b\u0448\u0435\u0431\u043d\u044b\u0445 \u0437\u0435\u043b\u0438\u0439? \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u043a\u0430\u043a \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u0443\u044e \u0438\u0433\u0440\u0443. <\/p>\n<p>\u041c\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f <a href=\"https:\/\/react-dnd.github.io\/react-dnd\/about\">react-dnd<\/a>, <a href=\"https:\/\/styled-components.com\/\">styled-components<\/a>,\u00a0<a href=\"https:\/\/mobx.js.org\/README.html\">mobx<\/a>\u00a0\u0438\u00a0<a href=\"https:\/\/ru.reactjs.org\/docs\/portals.html\">createPortal<\/a>.<\/p>\n<ol>\n<li>\n<p><a href=\"#1\">\u041f\u0440\u0430\u0432\u0438\u043b\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#2\">\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0441 \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#3\">\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#4\">\u0425\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u0438\u0433\u0440\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#5\">\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#6\">\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u0438 \u044f\u0447\u0435\u0439\u043a\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#7\">\u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c Drag&#8217;n&#8217;Drop<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#8\">\u0421\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441 \u0442\u0430\u0447-\u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#9\">\u0420\u0430\u0441\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u0438 \u0438\u0433\u0440\u0430\u0435\u043c<\/a><\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"1\" id=\"1\"><\/a><\/p>\n<h2>\u041f\u0440\u0430\u0432\u0438\u043b\u0430<\/h2>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/620048fccd09f7f6552eb58d\" data-style=\"\" id=\"620048fccd09f7f6552eb58d\" width=\"\"><\/div>\n<p>\u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c 5 \u0431\u0443\u0442\u044b\u043b\u043e\u043a \u0438 2 \u043f\u043e\u043b\u043a\u0438. \u041f\u0440\u0438 \u0441\u0442\u0430\u0440\u0442\u0435 \u0438\u0433\u0440\u044b \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u043b\u043a\u0435. \u0418\u0445 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043d\u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u043b\u043a\u0435.<\/p>\n<p><a class=\"anchor\" name=\"2\" id=\"2\"><\/a><\/p>\n<h2>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0441 \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438<\/h2>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c Drag&#8217;n&#8217;Drop \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u043a\u0435\u0442\u0430 <a href=\"https:\/\/react-dnd.github.io\/react-dnd\/about\">react-dnd<\/a>. \u041e\u043d \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f <code>drag<\/code>&#8212; \u0438 <code>drop<\/code>-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432. \u0412\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0445\u0443\u043a\u0438 <code>useDrag<\/code> \u0438 <code>useDrop<\/code>.<\/p>\n<p>React-dnd \u043d\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043d\u044b\u0435 \u0438 \u0442\u0430\u0447-\u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u043d\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 DnD-\u0431\u044d\u043a\u0435\u043d\u0434\u044b.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <a href=\"https:\/\/developer.mozilla.org\/ru\/docs\/Web\/API\/HTML_Drag_and_Drop_API\">HTML Drag and Drop API<\/a> \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c <a href=\"https:\/\/www.npmjs.com\/package\/react-dnd-html5-backend\">react-dnd-html5-backend<\/a>. \u0411\u0440\u0430\u0443\u0437\u0435\u0440\u043d\u044b\u0439 API \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043d\u0430\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043f\u0435\u0440\u0435\u0442\u044f\u0433\u0438\u0432\u0430\u043d\u0438\u044f, \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432 \u0438 \u0440\u0435\u043d\u0434\u0435\u0440\u0430 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u043d\u0438\u044f. \u0412 \u0431\u0438\u043b\u0434\u0435 \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0441\u044f HTML-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u043c draggable.<\/p>\n<p>\u0414\u043b\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0441 \u0442\u0430\u0447-\u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c \u0432\u044b\u0431\u0435\u0440\u0435\u043c <a href=\"https:\/\/developer.mozilla.org\/ru\/docs\/Web\/API\/Touch_events\">Touch events<\/a> \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c <a href=\"https:\/\/www.npmjs.com\/package\/react-dnd-touch-backend\">react-dnd-touch-backend<\/a>. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/ru.reactjs.org\/docs\/portals.html\">createPortal<\/a> \u0438 \u0441\u0442\u0438\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0445\u0443\u043a\u0430 <code>usePreview<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0430\u043a\u0435\u0442 <a href=\"https:\/\/www.npmjs.com\/package\/react-dnd-multi-backend\">react-dnd-multi-backend<\/a> \u0441\u0430\u043c \u0432\u044b\u0431\u0435\u0440\u0435\u0442, \u043a\u0430\u043a\u043e\u0439 DnD-\u0431\u044d\u043a\u0435\u043d\u0434 \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0435.<\/p>\n<p><a class=\"anchor\" name=\"3\" id=\"3\"><\/a><\/p>\n<h3>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u043c \u043d\u0430\u0448\u0438 \u0431\u0443\u0442\u044b\u043b\u043a\u0438, \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0431\u0443\u0442\u044b\u043b\u043e\u043a, \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0438 \u043f\u043e\u043b\u043a\u0438:<\/p>\n<pre><code class=\"javascript\">enum BottlesEnum { blue, brown, green, red, white, }  const bottles: Record&lt;BottlesEnum, BottleType> = { [BottlesEnum.blue]: { id: BottlesEnum.blue, image: require('.\/img\/blue.png'), }, \/*...*\/ }  const correctPositions = [ BottlesEnum.green, BottlesEnum.white, BottlesEnum.brown, BottlesEnum.red, BottlesEnum.blue, ];  enum ShelvesEnum { top, bottom, }<\/code><\/pre>\n<p>\u0422\u0435\u043a\u0441\u0442\u043e\u043c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438 \u0434\u043b\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"javascript\">const rules = [ '\u041f\u043e \u043a\u0440\u0430\u044f\u043c \u0441\u0442\u043e\u044f\u0442 \u043a\u0440\u0443\u0433\u043b\u044b\u0435 \u0431\u0443\u0442\u044b\u043b\u043a\u0438', '\u0413\u043e\u043b\u0443\u0431\u0430\u044f \u0431\u0443\u0442\u044b\u043b\u043a\u0430 \u0441\u0442\u043e\u0438\u0442 \u0440\u044f\u0434\u043e\u043c \u0441 \u043a\u0440\u0430\u0441\u043d\u043e\u0439', '\u0412 \u0446\u0435\u043d\u0442\u0440\u0435 \u0441\u0442\u043e\u0438\u0442 \u0431\u0443\u0442\u044b\u043b\u043a\u0430 \u0431\u0435\u0437 \u043f\u0440\u043e\u0431\u043a\u0438', '\u041a\u0440\u0430\u0441\u043d\u0430\u044f \u0431\u0443\u0442\u044b\u043b\u043a\u0430 \u0441\u0442\u043e\u0438\u0442 \u043f\u0440\u0430\u0432\u0435\u0435 \u0437\u0435\u043b\u0435\u043d\u043e\u0439 \u0438 \u0431\u0435\u043b\u043e\u0439', '\u0417\u0435\u043b\u0435\u043d\u0430\u044f \u0431\u0443\u0442\u044b\u043b\u043a\u0430 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0440\u044f\u0434\u043e\u043c \u0441 \u0431\u0443\u0442\u044b\u043b\u043a\u0430\u043c\u0438 \u0431\u0435\u0437 \u043f\u0440\u043e\u0431\u043a\u0438', ];<\/code><\/pre>\n<p><a class=\"anchor\" name=\"4\" id=\"4\"><\/a><\/p>\n<h2>\u0425\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u0438\u0433\u0440\u044b<\/h2>\n<p>\u0414\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0438 \u043b\u043e\u0433\u0438\u043a\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c MobX. \u041e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435, \u0438\u043b\u0438 \u0441\u0442\u043e\u0440, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435.<\/p>\n<p>\u0421\u0430\u043c\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u2014 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442. \u0412\u0441\u044f \u043c\u0430\u0433\u0438\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u044b \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043b\u0438\u044f\u044e\u0442 \u043d\u0430 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u043a\u0430\u043a \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c\u044b\u0435 (<code>observable<\/code>), \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u0442\u0435\u043b\u0435\u043c \u0441\u0430\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 (<code>observer<\/code>).<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0430 \u043f\u0435\u0440\u0435\u0440\u0438\u0441\u043e\u0432\u043a\u0443 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u043b\u0438\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 <code>observable<\/code>-\u043f\u043e\u043b\u0435\u0439. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c\u00a0<a href=\"https:\/\/mobx.js.org\/observable-state.html\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u0412 \u0441\u0442\u043e\u0440\u0435 \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u044f\u0447\u0435\u0435\u043a, \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c\u043e\u0439 \u044f\u0447\u0435\u0439\u043a\u0438, \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u043d\u0438\u044f, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 <code>onDrag\/onDrop<\/code> \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"javascript\">import { makeAutoObservable } from 'mobx'; import { createContext } from 'react';  type ShelfItemType = BottlesEnum | null; type ShelfItemsListType = ShelfItemType[];  class BottlesGameStore { draggedPosition: PositionType | null = null; \/\/ \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f drag-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u043d\u0438\u044f positions: Record&lt;ShelvesEnum, ShelfItemsListType>; \/\/ \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u044f\u0447\u0435\u0435\u043a \u043d\u0430 \u043f\u043e\u043b\u043a\u0430\u0445  constructor() { makeAutoObservable(this); this.shuffle(); }  shuffle(): void { \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 this.positions = { [ShelvesEnum.top]: new Array(correctPositions.length).fill(null), [ShelvesEnum.bottom]: [...correctPositions].sort( () => Math.random() - 0.5 ), };  this.isOneAtBottomCorrect &amp;&amp; this.shuffle(); \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u043c \u0435\u0449\u0435 \u0440\u0430\u0437, \u0435\u0441\u043b\u0438 \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u0430 \u0441\u0442\u043e\u0438\u0442 \u043d\u0430 \u043d\u0443\u0436\u043d\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 }  get isOneAtBottomCorrect(): boolean { return correctPositions.some( (bottleId, columnIndex) => bottleId === this.positions[ShelvesEnum.bottom][columnIndex] ); }  onDrag(position: PositionType): void { this.draggedPosition = position; }  onDrop(bottleId: number, position: PositionType): void { const itemAtDrop = this.getItem(position); \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0443 \u0432 drop-\u044f\u0447\u0435\u0439\u043a\u0435 if (itemAtDrop || !this.draggedPosition || this.isCorrect) { return; }  this.setItem(this.draggedPosition, null); \/\/ \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0443 \u0438\u0437 drag-\u044f\u0447\u0435\u0439\u043a\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u043d\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u043d\u0438\u044f this.setItem(position, bottleId); \/\/ \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0443 \u0432 drop-\u044f\u0447\u0435\u0439\u043a\u0443 }  getItem(position: PositionType): ShelfItemType { const [shelfIndex, columnIndex] = position; return this.positions[shelfIndex][columnIndex]; }  setItem(position: PositionType, item: ShelfItemType): void { const [shelfIndex, columnIndex] = position; this.positions[shelfIndex][columnIndex] = item; }    get isCorrect(): boolean { \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438   return (   JSON.stringify(correctPositions) ===   JSON.stringify(this.positions[ShelvesEnum.top]) \/\/ \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 ?   );   }    get isUncorrect(): boolean { \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043f\u043e\u043b\u043a\u0430 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430, \u043d\u043e \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u043d\u0435 \u0432\u0435\u0440\u043d\u044b return ( !this.isCorrect &amp;&amp; this.positions[ShelvesEnum.top].every((position) => position !== null) ); } }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0443 \u0438\u0437 \u043b\u044e\u0431\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0435\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442.<\/p>\n<pre><code class=\"javascript\">const BottlesGameContext = createContext&lt;BottlesGameStore | null>(null); \/\/ \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441 \u043d\u0430\u0448\u0438\u043c \u0441\u0442\u043e\u0440\u043e\u043c  const useStore = &lt;T>(context: React.Context&lt;T | null>): T => { \/\/ \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 const data = useContext(context); if (!data) { throw new Error('Using store outside of context'); } return data; };<\/code><\/pre>\n<p><a class=\"anchor\" name=\"5\" id=\"5\"><\/a><\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435<\/h2>\n<p>\u0414\u043b\u044f \u0441\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>styled-components<\/code>. \u042d\u0442\u043e CSS-in-JS \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0442\u0438\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u043f\u0441\u043e\u0432, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432.<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u0442\u0438\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438 \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 \u0441 \u043f\u043e\u043b\u043a\u0430\u043c\u0438 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c\u0438<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">const Container = styled.div` position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 120rem; height: 50rem; text-align: center; background: black; `;  const Playground = styled.div` position: absolute; width: 100%; height: 100%; `;  const Title = styled.div` font-size: 2.5rem; color: white; text-align: center; z-index: 1; font-family: monospace; `;  const Shelves = styled.div` width: 76.8rem; height: 29.83rem; position: absolute; background: url(${require(.\/img\/shelves.png)}) no-repeat center \/ contain; top: 0; bottom: 0; margin: auto; `;  const Rules = styled.ul` color: white; text-align: left; `;<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0441 \u0438\u0433\u0440\u043e\u0439. <\/p>\n<p>\u041e\u0431\u0435\u0440\u043d\u0451\u043c \u0432\u0441\u0435 \u0432 DnD-\u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u00ab\u0431\u044d\u043a\u0435\u043d\u0434\u00bb \u0438 \u043e\u043f\u0446\u0438\u0438. \u0422\u0430\u043a\u0436\u0435 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0435 MobX-\u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u0441 \u0435\u0433\u043e \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u043e\u043c. <\/p>\n<p>\u041d\u0430\u0440\u0438\u0441\u0443\u0435\u043c \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435, \u043f\u043e\u043b\u043a\u0438 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0438\u0437 \u043a\u043e\u043d\u0444\u0438\u0433\u0430.<\/p>\n<pre><code class=\"javascript\">import { DndProvider } from 'react-dnd'; import MultiBackend from 'react-dnd-multi-backend'; import HTML5toTouch from 'react-dnd-multi-backend\/dist\/esm\/HTML5toTouch';  const BottlesGame: React.FC = () => { const [store] = React.useState(() => new BottlesGameStore()); return ( &lt;DndProvider backend={MultiBackend} options={HTML5toTouch}> &lt;BottlesGameContext.Provider value={store}> &lt;Container> &lt;Playground> &lt;Title>\u0420\u0430\u0441\u0441\u0442\u0430\u0432\u044c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u043d\u0430 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u043f\u043e\u043b\u043a\u0435 \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435&lt;\/Title> &lt;Shelves \/> &lt;Rules> {rules.map((rule, i) => ( &lt;li key={i}>{rule}&lt;\/li> ))} &lt;\/Rules> &lt;\/Playground> &lt;\/Container> &lt;\/BottlesGameContext.Provider> &lt;\/DndProvider> ); };<\/code><\/pre>\n<p><a class=\"anchor\" name=\"6\" id=\"6\"><\/a><\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u0438 \u044f\u0447\u0435\u0439\u043a\u0438<\/h2>\n<p>\u0421\u0442\u0438\u043b\u0438\u0437\u0443\u0435\u043c \u044f\u0447\u0435\u0439\u043a\u0443 \u0441 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u043c \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u043e\u043b\u043a\u0438. \u042d\u0442\u043e\u0442 \u0441\u0442\u0438\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0449\u0438\u043c \u0434\u043b\u044f <code>drag<\/code>&#8212; \u0438 <code>drop<\/code>-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"javascript\">type PositionType = [ShelvesEnum, BottlesEnum];  const getShelfItemPositionStyle = ([top, left]: PositionType) => ` top: ${top * DROP_HEIGHT}rem; left: ${left * DROP_WIDTH + SHELF_START_H}rem; `;  const DNDItem = styled.div&lt;{ position: PositionType; }>` ${({ position }) => getShelfItemPositionStyle(position)} position: absolute; `;  const SHELF_START_H = 2.7; \/\/ \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0442\u0441\u0442\u0443\u043f \u043c\u0435\u0436\u0434\u0443 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u043f\u043e\u043b\u043a\u0438 \u0438 \u043f\u0435\u0440\u0432\u043e\u0439 \u044f\u0447\u0435\u0439\u043a\u043e\u0439 const DROP_WIDTH = 14.8; \/\/ \u0448\u0438\u0440\u0438\u043d\u0430 \u044f\u0447\u0435\u0439\u043a\u0438 \u043d\u0430 \u043f\u043e\u043b\u043a\u0435 const DROP_HEIGHT = 15; \/\/ \u0432\u044b\u0441\u043e\u0442\u0430 \u044f\u0447\u0435\u0439\u043a\u0438 \u043d\u0430 \u043f\u043e\u043b\u043a\u0435 const DRAG_SIZE = 12.1; \/\/ \u0448\u0438\u0440\u0438\u043d\u0430\/\u0432\u044b\u0441\u043e\u0442\u0430 draggable \u0431\u0443\u0442\u044b\u043b\u043a\u0438<\/code><\/pre>\n<p>\u0421\u0442\u0438\u043b\u0438\u0437\u0443\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0442\u044c (<code>drag<\/code>):<\/p>\n<pre><code class=\"javascript\">const BottleDragWrapper = styled(DNDItem)&lt;{ isDragging: boolean; }>` z-index: 1; width: ${DRAG_SIZE}rem; height: ${DRAG_SIZE}rem; background-repeat: no-repeat; background-position: center; background-size: contain;  \/* \u0412 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u044b\u0432\u0430\u043d\u0438\u044f \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 *\/ visibility: ${(props) => (props.isDragging ? 'hidden' : 'visible')}; `;<\/code><\/pre>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"496\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/113\/1e8\/b92\/1131e8b9249321cc9390e8d7e4b29c77.png\" data-width=\"1236\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0421\u0442\u0438\u043b\u0438\u0437\u0443\u0435\u043c \u044f\u0447\u0435\u0439\u043a\u0443, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u0431\u0440\u043e\u0441\u0430\u0442\u044c (<code>drop<\/code>) \u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u043d\u0438\u044f \u043d\u0430 \u0442\u0430\u0447-\u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u0445:<\/p>\n<pre><code class=\"javascript\">const BottleDropWrapper = styled(DNDItem)` width: ${DROP_WIDTH}rem; height: ${DROP_HEIGHT}rem; `;  const BottlePreviewImg = styled.img` width: ${DRAG_SIZE}rem; height: ${DRAG_SIZE}rem; `;<\/code><\/pre>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"496\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4cc\/a0c\/80a\/4cca0c80a88c69fd810ef29b185f3986.png\" data-width=\"1236\"\/><figcaption><\/figcaption><\/figure>\n<p><a class=\"anchor\" name=\"7\" id=\"7\"><\/a><\/p>\n<h2>\u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c Drag&#8217;n&#8217;Drop<\/h2>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 BottleDrag \u2014 \u0431\u0443\u0442\u044b\u043b\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0442\u044f\u0433\u0438\u0432\u0430\u0442\u044c.<\/p>\n<p>\u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0445\u0443\u043a\u043e\u043c <a href=\"https:\/\/react-dnd.github.io\/react-dnd\/docs\/api\/use-drag\">useDrag<\/a> \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 react-dnd. \u0412 \u043d\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0442\u0438\u043f \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 (\u0443 \u043d\u0430\u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e BOTTLE_DND_TYPE) \u0438 \u043a\u043e\u043d\u0444\u0438\u0433 \u0431\u0443\u0442\u044b\u043b\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 react-dnd \u0438 \u0434\u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0432 drop-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a onDrag \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u0435\u0440\u0435\u0442\u044f\u0433\u0438\u0432\u0430\u043d\u0438\u044f. \u0418\u0437 \u0445\u0443\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 <code>isDragging<\/code> \u0438 <code>ref<\/code> \u0434\u043b\u044f DOM-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.<\/p>\n<pre><code class=\"javascript\">import { useDrag } from 'react-dnd';  type BottleDragProps = { bottle: BottleType; position: PositionType; };  const BOTTLE_DND_TYPE = 'BOTTLE_DND_TYPE';  const BottleDrag: React.FC&lt;BottleDragProps> = ({ bottle, position, }: BottleDragProps) => {  const store = useStore(BottlesGameContext);  const [ { isDragging }, drag \/\/ ref drag-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 ] = useDrag({ item: { type: BOTTLE_DND_TYPE, \/\/ \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0438\u043f\u0430 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0445 drag \u0438 drop \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043e\u0434\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 bottle \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 }, collect: (monitor) => ({ isDragging: monitor.isDragging(), \/\/ \u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0440\u0435\u043d\u0434\u0435\u0440\u0430 }), begin: () => store.onDrag(position), });  return ( &lt;BottleDragWrapper ref={drag} position={position} isDragging={isDragging} style={{ backgroundImage: `url(${bottle.image})`, }} \/> ); };<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 BottleDrop \u2014 \u044f\u0447\u0435\u0439\u043a\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0442\u044f\u0433\u0438\u0432\u0430\u0442\u044c \u0431\u0443\u0442\u044b\u043b\u043a\u0443.<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0445\u0443\u043a\u043e\u043c <a href=\"https:\/\/react-dnd.github.io\/react-dnd\/docs\/api\/use-drop\">useDrop<\/a>. \u0412 \u043d\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0442\u0438\u043f \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a <code>onDrop<\/code> \u0438\u0437 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430. \u0418\u0437 \u0445\u0443\u043a\u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c <code>ref<\/code> \u0434\u043b\u044f DOM-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.<\/p>\n<pre><code class=\"javascript\">type BottleDropProps = { position: PositionType; };  const BottleDrop: React.FC&lt;BottleDropProps> = ({ position, }: BottleDropProps) => { const store = useStore(BottlesGameContext); const [, drop \/\/ ref drop-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 ] = useDrop({ accept: BOTTLE_DND_TYPE, \/\/ \u0432 \u044d\u0442\u043e\u0442 drop-\u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0442\u0430\u0449\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e drag-\u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c \u0442\u0438\u043f\u043e\u043c* drop: (item) => { store.onDrop(item.bottle.id, position); }, });  return &lt;BottleDropWrapper position={position} ref={drop} \/>; };<\/code><\/pre>\n<p><a class=\"anchor\" name=\"8\" id=\"8\"><\/a><\/p>\n<h2>\u0421\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441 \u0442\u0430\u0447-\u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438<\/h2>\n<p>\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0434\u0435\u0441\u043a\u0442\u043e\u043f\u043d\u044b\u0445, \u0432 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430\u0445 \u043f\u0435\u0440\u0435\u0442\u044f\u0433\u0438\u0432\u0430\u043d\u0438\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0435 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u043f\u0435\u0440\u0435\u0442\u044f\u0433\u0438\u0432\u0430\u043d\u0438\u044f, \u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0436\u0435\u0441\u0442\u043e\u0432. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0430\u043c\u0438\u043c. <\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c DndPreview \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/ru.reactjs.org\/docs\/portals.html\">createPortal<\/a>. <\/p>\n<p><em>\u041f\u043e\u0440\u0442\u0430\u043b\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u00a0DOM-\u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432\u043d\u0435 DOM-\u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430.<\/em><\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0443\u0437\u0435\u043b \u0440\u044f\u0434\u043e\u043c \u0441 \u0443\u0437\u043b\u043e\u043c \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u2014 \u0432 \u043d\u0451\u043c \u0431\u0443\u0434\u0435\u043c \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 <code>drag<\/code>-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0447\u0435\u0440\u0435\u0437 <code>inline<\/code>-\u0441\u0442\u0438\u043b\u044c.<\/p>\n<pre><code class=\"javascript\">type DndPreviewPortalProps = { children: React.ReactNode; display: boolean };  const createDndElement = () => { const el = document.createElement('div'); el.className = 'dnd-item'; return el; };  const DndPreviewPortal: React.FC&lt;DndPreviewPortalProps> = ({ children, display, }: DndPreviewPortalProps) => { const el = useRef(createDndElement()).current;  useEffect(() => { display ? document.body.appendChild(el) : document.body.removeChild(el); }, [display, el]);  useEffect(() => { return () => { document.body.removeChild(el); }; }, [el]);  if (!display) { return null; }  return createPortal(children, el); };<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 BottlePreview \u0441 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c <code>drag<\/code>-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.<\/p>\n<p>\u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0445\u0443\u043a\u043e\u043c <a href=\"https:\/\/github.com\/LouisBrunner\/dnd-multi-backend\/tree\/main\/packages\/react-dnd-preview\">usePreview<\/a>. \u0418\u0437 \u043d\u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 <code>display<\/code>, \u043a\u043e\u043d\u0444\u0438\u0433 \u0431\u0443\u0442\u044b\u043b\u043a\u0438 <code>item<\/code> \u0438\u0437 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 react-dnd, <code>style<\/code> \u0441 \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0438 <code>ref<\/code> DOM-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.<\/p>\n<pre><code class=\"javascript\">import { usePreview } from 'react-dnd-multi-backend';  const BottlePreview: React.FC = () => { const { display, item, style, ref } = usePreview(); if (!display || item.type !== BOTTLE_DND_TYPE) { return null; }  return ( &lt;DndPreview display={display}> &lt;BottlePreviewImg src={item.bottle.image} style={style} ref={ref} \/> &lt;\/DndPreview> ); };<\/code><\/pre>\n<p><a class=\"anchor\" name=\"9\" id=\"9\"><\/a><\/p>\n<h2>\u0420\u0430\u0441\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u0438 \u0438\u0433\u0440\u0430\u0435\u043c<\/h2>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0435 <code>Drag<\/code>-, <code>Drop<\/code>&#8212; \u0438 <code>Preview<\/code>-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0432 \u0438\u0433\u0440\u0443.<\/p>\n<pre><code class=\"javascript\">const BottlesGame: React.FC = () => { \/* ... *\/ &lt;Shelves> &lt;BottlePreview \/>    {store.positionKeys.map((shelfKey, shelfIndex) => \/\/ \u043f\u0440\u043e\u0431\u0435\u0433\u0430\u0435\u043c\u0441\u044f \u043f\u043e \u043f\u043e\u0437\u0438\u0446\u0438\u044f\u043c     store.getShelf(shelfKey).map((bottleKey, bottleIndex) => { \/\/ \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u043d\u0430 \u043f\u043e\u043b\u043a\u0430\u0445       if (bottleKey === null) {         return null;       }       return (       &lt;BottleDrag         bottle={bottles[bottleKey]}         position={[shelfIndex, bottleIndex]}           key={`bottle-${shelfKey}-${bottleKey}`} \/>   );      })   )}      {Object.keys(bottles).map((columnIndex) => ( \/\/ \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043c \u043f\u043e \u044f\u0447\u0435\u0439\u043a\u0435 \u0434\u043b\u044f \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u043e\u043b\u043a\u0435   &lt;div key={`bottlePlaceholder-${columnIndex}`}>   &lt;BottleDrop position={[ShelvesEnum.top, Number(columnIndex)]} \/>   &lt;BottleDrop   position={[ShelvesEnum.bottom, Number(columnIndex)]}   \/>   &lt;\/div>   ))} &lt;\/Shelves> \/* ... *\/ };<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043e \u0432\u0435\u0440\u043d\u043e\u043c \u0438 \u043d\u0435\u0432\u0435\u0440\u043d\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\u0445:<\/p>\n<pre><code class=\"javascript\">const BottlesGame: React.FC = () => { \/* ... *\/ &lt;Playground> {store.isUncorrect ? ( &lt;Title>\u042d\u0442\u0430 \u0440\u0430\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043d\u0435\u0432\u0435\u0440\u043d\u0430\u044f, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439 \u0435\u0449\u0435 \u0440\u0430\u0437&lt;\/Title> ) : ( &lt;Title>\u0420\u0430\u0441\u0441\u0442\u0430\u0432\u044c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u043d\u0430 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u043f\u043e\u043b\u043a\u0435 \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435&lt;\/Title> )} &lt;\/Playground> {store.isCorrect &amp;&amp; &lt;div>Success!&lt;\/div>} \/* ... *\/ };<\/code><\/pre>\n<p>\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u044e, \u0438\u0433\u0440\u0430 \u0433\u043e\u0442\u043e\u0432\u0430!<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"706\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/849\/e16\/9ea\/849e169ea602f5f93eed7a67f1ffb297.png\" data-width=\"1236\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0413\u043e\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u043b\u0435\u0436\u0438\u0442 <a href=\"https:\/\/codesandbox.io\/s\/react-drag-drop-bottles-wcpl5\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u041f\u043e\u0438\u0433\u0440\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/wcpl5.csb.app\/\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u0412 \u043c\u043e\u0435\u0439 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/company\/kts\/blog\/580198\/\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043c\u0438\u043d\u0438-\u0438\u0433\u0440\u044b \u00ab\u0428\u043a\u0430\u0442\u0443\u043b\u043a\u0430\u00bb<\/a> \u043c\u043e\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e MobX-\u0441\u0442\u043e\u0440\u0430\u0445 \u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0430\u0443\u0434\u0438\u043e\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 Howler.<\/p>\n<p>\u0414\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0438!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/kts\/blog\/647241\/\"> https:\/\/habr.com\/ru\/company\/kts\/blog\/647241\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442!<\/p>\n<p>\u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0421\u0435\u0440\u0433\u0435\u0439, \u044f \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043e\u0442\u0434\u0435\u043b\u0430 \u0441\u043f\u0435\u0446\u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 <a href=\"https:\/\/kts.studio\">KTS<\/a>. \u041d\u0430\u0448 \u043e\u0442\u0434\u0435\u043b \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043f\u0440\u043e\u043c\u043e\u043a\u0430\u043c\u043f\u0430\u043d\u0438\u0439.\u00a0<\/p>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u043a\u0430\u043a \u0432 1-\u0439 \u043a\u043d\u0438\u0433\u0435 \u043e \u0413\u0430\u0440\u0440\u0438 \u041f\u043e\u0442\u0442\u0435\u0440\u0435 \u0413\u0435\u0440\u043c\u0438\u043e\u043d\u0430 \u0440\u0430\u0437\u0433\u0430\u0434\u044b\u0432\u0430\u043b\u0430 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0437\u0430\u0433\u0430\u0434\u043a\u0443  \u0441 \u0431\u0443\u0442\u044b\u043b\u043e\u0447\u043a\u0430\u043c\u0438 \u0432\u043e\u043b\u0448\u0435\u0431\u043d\u044b\u0445 \u0437\u0435\u043b\u0438\u0439? \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u043a\u0430\u043a \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u0443\u044e \u0438\u0433\u0440\u0443. <\/p>\n<p>\u041c\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f <a href=\"https:\/\/react-dnd.github.io\/react-dnd\/about\">react-dnd<\/a>, <a href=\"https:\/\/styled-components.com\/\">styled-components<\/a>,\u00a0<a href=\"https:\/\/mobx.js.org\/README.html\">mobx<\/a>\u00a0\u0438\u00a0<a href=\"https:\/\/ru.reactjs.org\/docs\/portals.html\">createPortal<\/a>.<\/p>\n<ol>\n<li>\n<p><a href=\"#1\">\u041f\u0440\u0430\u0432\u0438\u043b\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#2\">\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0441 \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#3\">\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#4\">\u0425\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u0438\u0433\u0440\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#5\">\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#6\">\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u0438 \u044f\u0447\u0435\u0439\u043a\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#7\">\u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c Drag&#8217;n&#8217;Drop<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#8\">\u0421\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441 \u0442\u0430\u0447-\u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#9\">\u0420\u0430\u0441\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u0438 \u0438\u0433\u0440\u0430\u0435\u043c<\/a><\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"1\" id=\"1\"><\/a><\/p>\n<h2>\u041f\u0440\u0430\u0432\u0438\u043b\u0430<\/h2>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/620048fccd09f7f6552eb58d\" data-style=\"\" id=\"620048fccd09f7f6552eb58d\" width=\"\"><\/div>\n<p>\u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c 5 \u0431\u0443\u0442\u044b\u043b\u043e\u043a \u0438 2 \u043f\u043e\u043b\u043a\u0438. \u041f\u0440\u0438 \u0441\u0442\u0430\u0440\u0442\u0435 \u0438\u0433\u0440\u044b \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u043b\u043a\u0435. \u0418\u0445 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043d\u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u043b\u043a\u0435.<\/p>\n<p><a class=\"anchor\" name=\"2\" id=\"2\"><\/a><\/p>\n<h2>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0441 \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438<\/h2>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c Drag&#8217;n&#8217;Drop \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u043a\u0435\u0442\u0430 <a href=\"https:\/\/react-dnd.github.io\/react-dnd\/about\">react-dnd<\/a>. \u041e\u043d \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f <code>drag<\/code>&#8212; \u0438 <code>drop<\/code>-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432. \u0412\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0445\u0443\u043a\u0438 <code>useDrag<\/code> \u0438 <code>useDrop<\/code>.<\/p>\n<p>React-dnd \u043d\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043d\u044b\u0435 \u0438 \u0442\u0430\u0447-\u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u043d\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 DnD-\u0431\u044d\u043a\u0435\u043d\u0434\u044b.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <a href=\"https:\/\/developer.mozilla.org\/ru\/docs\/Web\/API\/HTML_Drag_and_Drop_API\">HTML Drag and Drop API<\/a> \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c <a href=\"https:\/\/www.npmjs.com\/package\/react-dnd-html5-backend\">react-dnd-html5-backend<\/a>. \u0411\u0440\u0430\u0443\u0437\u0435\u0440\u043d\u044b\u0439 API \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043d\u0430\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043f\u0435\u0440\u0435\u0442\u044f\u0433\u0438\u0432\u0430\u043d\u0438\u044f, \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432 \u0438 \u0440\u0435\u043d\u0434\u0435\u0440\u0430 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u043d\u0438\u044f. \u0412 \u0431\u0438\u043b\u0434\u0435 \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0441\u044f HTML-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u043c draggable.<\/p>\n<p>\u0414\u043b\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0441 \u0442\u0430\u0447-\u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c \u0432\u044b\u0431\u0435\u0440\u0435\u043c <a href=\"https:\/\/developer.mozilla.org\/ru\/docs\/Web\/API\/Touch_events\">Touch events<\/a> \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c <a href=\"https:\/\/www.npmjs.com\/package\/react-dnd-touch-backend\">react-dnd-touch-backend<\/a>. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/ru.reactjs.org\/docs\/portals.html\">createPortal<\/a> \u0438 \u0441\u0442\u0438\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0445\u0443\u043a\u0430 <code>usePreview<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0430\u043a\u0435\u0442 <a href=\"https:\/\/www.npmjs.com\/package\/react-dnd-multi-backend\">react-dnd-multi-backend<\/a> \u0441\u0430\u043c \u0432\u044b\u0431\u0435\u0440\u0435\u0442, \u043a\u0430\u043a\u043e\u0439 DnD-\u0431\u044d\u043a\u0435\u043d\u0434 \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0435.<\/p>\n<p><a class=\"anchor\" name=\"3\" id=\"3\"><\/a><\/p>\n<h3>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u043c \u043d\u0430\u0448\u0438 \u0431\u0443\u0442\u044b\u043b\u043a\u0438, \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0431\u0443\u0442\u044b\u043b\u043e\u043a, \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0438 \u043f\u043e\u043b\u043a\u0438:<\/p>\n<pre><code class=\"javascript\">enum BottlesEnum { blue, brown, green, red, white, }  const bottles: Record&lt;BottlesEnum, BottleType> = { [BottlesEnum.blue]: { id: BottlesEnum.blue, image: require('.\/img\/blue.png'), }, \/*...*\/ }  const correctPositions = [ BottlesEnum.green, BottlesEnum.white, BottlesEnum.brown, BottlesEnum.red, BottlesEnum.blue, ];  enum ShelvesEnum { top, bottom, }<\/code><\/pre>\n<p>\u0422\u0435\u043a\u0441\u0442\u043e\u043c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438 \u0434\u043b\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"javascript\">const rules = [ '\u041f\u043e \u043a\u0440\u0430\u044f\u043c \u0441\u0442\u043e\u044f\u0442 \u043a\u0440\u0443\u0433\u043b\u044b\u0435 \u0431\u0443\u0442\u044b\u043b\u043a\u0438', '\u0413\u043e\u043b\u0443\u0431\u0430\u044f \u0431\u0443\u0442\u044b\u043b\u043a\u0430 \u0441\u0442\u043e\u0438\u0442 \u0440\u044f\u0434\u043e\u043c \u0441 \u043a\u0440\u0430\u0441\u043d\u043e\u0439', '\u0412 \u0446\u0435\u043d\u0442\u0440\u0435 \u0441\u0442\u043e\u0438\u0442 \u0431\u0443\u0442\u044b\u043b\u043a\u0430 \u0431\u0435\u0437 \u043f\u0440\u043e\u0431\u043a\u0438', '\u041a\u0440\u0430\u0441\u043d\u0430\u044f \u0431\u0443\u0442\u044b\u043b\u043a\u0430 \u0441\u0442\u043e\u0438\u0442 \u043f\u0440\u0430\u0432\u0435\u0435 \u0437\u0435\u043b\u0435\u043d\u043e\u0439 \u0438 \u0431\u0435\u043b\u043e\u0439', '\u0417\u0435\u043b\u0435\u043d\u0430\u044f \u0431\u0443\u0442\u044b\u043b\u043a\u0430 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0440\u044f\u0434\u043e\u043c \u0441 \u0431\u0443\u0442\u044b\u043b\u043a\u0430\u043c\u0438 \u0431\u0435\u0437 \u043f\u0440\u043e\u0431\u043a\u0438', ];<\/code><\/pre>\n<p><a class=\"anchor\" name=\"4\" id=\"4\"><\/a><\/p>\n<h2>\u0425\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u0438\u0433\u0440\u044b<\/h2>\n<p>\u0414\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0438 \u043b\u043e\u0433\u0438\u043a\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c MobX. \u041e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435, \u0438\u043b\u0438 \u0441\u0442\u043e\u0440, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435.<\/p>\n<p>\u0421\u0430\u043c\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u2014 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442. \u0412\u0441\u044f \u043c\u0430\u0433\u0438\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u044b \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043b\u0438\u044f\u044e\u0442 \u043d\u0430 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u043a\u0430\u043a \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c\u044b\u0435 (<code>observable<\/code>), \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u0442\u0435\u043b\u0435\u043c \u0441\u0430\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 (<code>observer<\/code>).<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0430 \u043f\u0435\u0440\u0435\u0440\u0438\u0441\u043e\u0432\u043a\u0443 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u043b\u0438\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 <code>observable<\/code>-\u043f\u043e\u043b\u0435\u0439. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c\u00a0<a href=\"https:\/\/mobx.js.org\/observable-state.html\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u0412 \u0441\u0442\u043e\u0440\u0435 \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u044f\u0447\u0435\u0435\u043a, \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c\u043e\u0439 \u044f\u0447\u0435\u0439\u043a\u0438, \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u043d\u0438\u044f, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 <code>onDrag\/onDrop<\/code> \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"javascript\">import { makeAutoObservable } from 'mobx'; import { createContext } from 'react';  type ShelfItemType = BottlesEnum | null; type ShelfItemsListType = ShelfItemType[];  class BottlesGameStore { draggedPosition: PositionType | null = null; \/\/ \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f drag-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u043d\u0438\u044f positions: Record&lt;ShelvesEnum, ShelfItemsListType>; \/\/ \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u044f\u0447\u0435\u0435\u043a \u043d\u0430 \u043f\u043e\u043b\u043a\u0430\u0445  constructor() { makeAutoObservable(this); this.shuffle(); }  shuffle(): void { \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 this.positions = { [ShelvesEnum.top]: new Array(correctPositions.length).fill(null), [ShelvesEnum.bottom]: [...correctPositions].sort( () => Math.random() - 0.5 ), };  this.isOneAtBottomCorrect &amp;&amp; this.shuffle(); \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u043c \u0435\u0449\u0435 \u0440\u0430\u0437, \u0435\u0441\u043b\u0438 \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u0430 \u0441\u0442\u043e\u0438\u0442 \u043d\u0430 \u043d\u0443\u0436\u043d\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 }  get isOneAtBottomCorrect(): boolean { return correctPositions.some( (bottleId, columnIndex) => bottleId === this.positions[ShelvesEnum.bottom][columnIndex] ); }  onDrag(position: PositionType): void { this.draggedPosition = position; }  onDrop(bottleId: number, position: PositionType): void { const itemAtDrop = this.getItem(position); \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0443 \u0432 drop-\u044f\u0447\u0435\u0439\u043a\u0435 if (itemAtDrop || !this.draggedPosition || this.isCorrect) { return; }  this.setItem(this.draggedPosition, null); \/\/ \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0443 \u0438\u0437 drag-\u044f\u0447\u0435\u0439\u043a\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u043d\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u043d\u0438\u044f this.setItem(position, bottleId); \/\/ \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0443 \u0432 drop-\u044f\u0447\u0435\u0439\u043a\u0443 }  getItem(position: PositionType): ShelfItemType { const [shelfIndex, columnIndex] = position; return this.positions[shelfIndex][columnIndex]; }  setItem(position: PositionType, item: ShelfItemType): void { const [shelfIndex, columnIndex] = position; this.positions[shelfIndex][columnIndex] = item; }    get isCorrect(): boolean { \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438   return (   JSON.stringify(correctPositions) ===   JSON.stringify(this.positions[ShelvesEnum.top]) \/\/ \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 ?   );   }    get isUncorrect(): boolean { \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043f\u043e\u043b\u043a\u0430 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430, \u043d\u043e \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u043d\u0435 \u0432\u0435\u0440\u043d\u044b return ( !this.isCorrect &amp;&amp; this.positions[ShelvesEnum.top].every((position) => position !== null) ); } }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0443 \u0438\u0437 \u043b\u044e\u0431\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0435\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442.<\/p>\n<pre><code class=\"javascript\">const BottlesGameContext = createContext&lt;BottlesGameStore | null>(null); \/\/ \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441 \u043d\u0430\u0448\u0438\u043c \u0441\u0442\u043e\u0440\u043e\u043c  const useStore = &lt;T>(context: React.Context&lt;T | null>): T => { \/\/ \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 const data = useContext(context); if (!data) { throw new Error('Using store outside of context'); } return data; };<\/code><\/pre>\n<p><a class=\"anchor\" name=\"5\" id=\"5\"><\/a><\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435<\/h2>\n<p>\u0414\u043b\u044f \u0441\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>styled-components<\/code>. \u042d\u0442\u043e CSS-in-JS \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0442\u0438\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u043f\u0441\u043e\u0432, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432.<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u0442\u0438\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438 \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 \u0441 \u043f\u043e\u043b\u043a\u0430\u043c\u0438 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c\u0438<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">const Container = styled.div` position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 120rem; height: 50rem; text-align: center; background: black; `;  const Playground = styled.div` position: absolute; width: 100%; height: 100%; `;  const Title = styled.div` font-size: 2.5rem; color: white; text-align: center; z-index: 1; font-family: monospace; `;  const Shelves = styled.div` width: 76.8rem; height: 29.83rem; position: absolute; background: url(${require(.\/img\/shelves.png)}) no-repeat center \/ contain; top: 0; bottom: 0; margin: auto; `;  const Rules = styled.ul` color: white; text-align: left; `;<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0441 \u0438\u0433\u0440\u043e\u0439. <\/p>\n<p>\u041e\u0431\u0435\u0440\u043d\u0451\u043c \u0432\u0441\u0435 \u0432 DnD-\u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u00ab\u0431\u044d\u043a\u0435\u043d\u0434\u00bb \u0438 \u043e\u043f\u0446\u0438\u0438. \u0422\u0430\u043a\u0436\u0435 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0435 MobX-\u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u0441 \u0435\u0433\u043e \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u043e\u043c. <\/p>\n<p>\u041d\u0430\u0440\u0438\u0441\u0443\u0435\u043c \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435, \u043f\u043e\u043b\u043a\u0438 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0438\u0437 \u043a\u043e\u043d\u0444\u0438\u0433\u0430.<\/p>\n<pre><code class=\"javascript\">import { DndProvider } from 'react-dnd'; import MultiBackend from 'react-dnd-multi-backend'; import HTML5toTouch from 'react-dnd-multi-backend\/dist\/esm\/HTML5toTouch';  const BottlesGame: React.FC = () => { const [store] = React.useState(() => new BottlesGameStore()); return ( &lt;DndProvider backend={MultiBackend} options={HTML5toTouch}> &lt;BottlesGameContext.Provider value={store}> &lt;Container> &lt;Playground> &lt;Title>\u0420\u0430\u0441\u0441\u0442\u0430\u0432\u044c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u043d\u0430 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u043f\u043e\u043b\u043a\u0435 \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435&lt;\/Title> &lt;Shelves \/> &lt;Rules> {rules.map((rule, i) => ( &lt;li key={i}>{rule}&lt;\/li> ))} &lt;\/Rules> &lt;\/Playground> &lt;\/Container> &lt;\/BottlesGameContext.Provider> &lt;\/DndProvider> ); };<\/code><\/pre>\n<p><a class=\"anchor\" name=\"6\" id=\"6\"><\/a><\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u0438 \u044f\u0447\u0435\u0439\u043a\u0438<\/h2>\n<p>\u0421\u0442\u0438\u043b\u0438\u0437\u0443\u0435\u043c \u044f\u0447\u0435\u0439\u043a\u0443 \u0441 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u043c \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u043e\u043b\u043a\u0438. \u042d\u0442\u043e\u0442 \u0441\u0442\u0438\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0449\u0438\u043c \u0434\u043b\u044f <code>drag<\/code>&#8212; \u0438 <code>drop<\/code>-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"javascript\">type PositionType = [ShelvesEnum, BottlesEnum];  const getShelfItemPositionStyle = ([top, left]: PositionType) => ` top: ${top * DROP_HEIGHT}rem; left: ${left * DROP_WIDTH + SHELF_START_H}rem; `;  const DNDItem = styled.div&lt;{ position: PositionType; }>` ${({ position }) => getShelfItemPositionStyle(position)} position: absolute; `;  const SHELF_START_H = 2.7; \/\/ \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0442\u0441\u0442\u0443\u043f \u043c\u0435\u0436\u0434\u0443 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u043f\u043e\u043b\u043a\u0438 \u0438 \u043f\u0435\u0440\u0432\u043e\u0439 \u044f\u0447\u0435\u0439\u043a\u043e\u0439 const DROP_WIDTH = 14.8; \/\/ \u0448\u0438\u0440\u0438\u043d\u0430 \u044f\u0447\u0435\u0439\u043a\u0438 \u043d\u0430 \u043f\u043e\u043b\u043a\u0435 const DROP_HEIGHT = 15; \/\/ \u0432\u044b\u0441\u043e\u0442\u0430 \u044f\u0447\u0435\u0439\u043a\u0438 \u043d\u0430 \u043f\u043e\u043b\u043a\u0435 const DRAG_SIZE = 12.1; \/\/ \u0448\u0438\u0440\u0438\u043d\u0430\/\u0432\u044b\u0441\u043e\u0442\u0430 draggable \u0431\u0443\u0442\u044b\u043b\u043a\u0438<\/code><\/pre>\n<p>\u0421\u0442\u0438\u043b\u0438\u0437\u0443\u0435\u043c \u0431\u0443\u0442\u044b\u043b\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0442\u044c (<code>drag<\/code>):<\/p>\n<pre><code class=\"javascript\">const BottleDragWrapper = styled(DNDItem)&lt;{ isDragging: boolean; }>` z-index: 1; width: ${DRAG_SIZE}rem; height: ${DRAG_SIZE}rem; background-repeat: no-repeat; background-position: center; background-size: contain;  \/* \u0412 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u044b\u0432\u0430\u043d\u0438\u044f \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 *\/ visibility: ${(props) => (props.isDragging ? 'hidden' : 'visible')}; `;<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0421\u0442\u0438\u043b\u0438\u0437\u0443\u0435\u043c \u044f\u0447\u0435\u0439\u043a\u0443, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u0431\u0440\u043e\u0441\u0430\u0442\u044c (<code>drop<\/code>) \u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u043d\u0438\u044f \u043d\u0430 \u0442\u0430\u0447-\u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u0445:<\/p>\n<pre><code class=\"javascript\">const BottleDropWrapper = styled(DNDItem)` width: ${DROP_WIDTH}rem; height: ${DROP_HEIGHT}rem; `;  const BottlePreviewImg = styled.img` width: ${DRAG_SIZE}rem; height: ${DRAG_SIZE}rem; `;<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><a class=\"anchor\" name=\"7\" id=\"7\"><\/a><\/p>\n<h2>\u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c Drag&#8217;n&#8217;Drop<\/h2>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 BottleDrag \u2014 \u0431\u0443\u0442\u044b\u043b\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0442\u044f\u0433\u0438\u0432\u0430\u0442\u044c.<\/p>\n<p>\u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0445\u0443\u043a\u043e\u043c <a href=\"https:\/\/react-dnd.github.io\/react-dnd\/docs\/api\/use-drag\">useDrag<\/a> \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 react-dnd. \u0412 \u043d\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0442\u0438\u043f \u043f\u0435\u0440\u0435\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 (\u0443 \u043d\u0430\u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e BOTTLE_DND_TYPE) \u0438 \u043a\u043e\u043d\u0444\u0438\u0433 \u0431\u0443\u0442\u044b\u043b\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 react-dnd \u0438 \u0434\u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0432 drop-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a onDrag \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u0435\u0440\u0435\u0442\u044f\u0433\u0438\u0432\u0430\u043d\u0438\u044f. \u0418\u0437 \u0445\u0443\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 <code>isDragging<\/code> \u0438 <code>ref<\/code> \u0434\u043b\u044f DOM-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.<\/p>\n<pre><code class=\"javascript\">import { useDrag } from 'react-dnd';  type BottleDragProps = { bottle: BottleType; position: PositionType; };  const BOTTLE_DND_TYPE = 'BOTTLE_DND_TYPE';  const BottleDrag: React.FC&lt;BottleDragProps> = ({ bottle, position, }: BottleDragProps) => {  const store = useStore(BottlesGameContext);  const [ { isDragging }, drag \/\/ ref drag-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 ] = useDrag({ item: { type: BOTTLE_DND_TYPE, \/\/ \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0438\u043f\u0430 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0445 drag \u0438 drop \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043e\u0434\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 bottle \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0431\u0443\u0442\u044b\u043b\u043a\u0438 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 }, collect: (monitor) => ({ isDragging: monitor.isDragging(), \/\/ \u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0440\u0435\u043d\u0434\u0435\u0440\u0430 }), begin: () => store.onDrag(position), });  return ( &lt;BottleDragWrapper ref={drag} position={position} isDragging={isDragging}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-329567","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/329567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=329567"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/329567\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=329567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=329567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=329567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}