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.

Cant get basic version working in React?

See original GitHub issue

I’m not sure if Im missing something basic but the following does nothing. I would expect something to get logged to the console when I tried to drag?

import React from "react";
import Moveable from "react-moveable";

class SomeComponent extends React.Component {
  render() {
    return (
      <React.Fragment>
        <div className="draggable">
          <div>Drag me</div>
        </div>
        <Moveable
          target={document.querySelector(".draggable")}
          draggable={true}
          throttleDrag={0}
          onDragStart={res => {
            console.log(res);
          }}
          onDrag={res => {
            console.log(res);
          }}
        />
      </React.Fragment>
    );
  }
}

export default SomeComponent;

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
jameschetwoodcommented, Oct 26, 2019

Quickly reopening just so I can share some code of this working incase it helps someone else finding this issue. Here is the codepen link: https://codesandbox.io/s/recursing-archimedes-jzw6p

function App() {
  return (
    <div className="App">
      <ThingToMove />
    </div>
  );
}

const ThingToMove = () => {
  const moveRef = React.useRef(null);
  const [style, setStyle] = React.useState("");

  return (
    <div>
      <h2
        ref={moveRef}
        style={{
          transform: style
        }}
      >
        Move me
      </h2>
      <MovableComponent moveRef={moveRef} setStyle={setStyle} />
    </div>
  );
};

const Movable = ({ moveRef, setStyle }) => {
  const [renderMovable, settRenderMovable] = React.useState(false);

  React.useEffect(() => {
    settRenderMovable(true);
  }, []);

  const handleDrag = e => {
    setStyle(e.transform);
  };

  if (!renderMovable) return null;

  return (
    <Moveable
      target={moveRef.current}
      draggable={true}
      throttleDrag={0}
      onDrag={handleDrag}
    />
  );
};
0reactions
jordie23commented, Apr 26, 2020

@jameschetwood thank you! None of the examples on the repo are actually helpful.

Yours works with 1 modification:

const Movable = ({ moveRef, setStyle }) => {

should be

const MovableComponent = ({ moveRef, setStyle }) => {

Here is a full working example that can be pasted into App.js of a new react-create-app app:

import React from "react";
import Moveable from "react-moveable";

function App() {
  return (
    <div className="App">
      <ThingToMove />
    </div>
  );
}

const ThingToMove = () => {
  const moveRef = React.useRef(null);
  const [style, setStyle] = React.useState("");

  return (
    <div>
      <h2
        ref={moveRef}
        style={{
          transform: style
        }}
      >
        Move me
      </h2>
      <MovableComponent moveRef={moveRef} setStyle={setStyle} />
    </div>
  );
};

const MovableComponent = ({ moveRef, setStyle }) => {
  const [renderMovable, settRenderMovable] = React.useState(false);

  React.useEffect(() => {
    settRenderMovable(true);
  }, []);

  const handleDrag = e => {
    setStyle(e.transform);
  };

  if (!renderMovable) return null;

  return (
    <Moveable
      target={moveRef.current}
      draggable={true}
      throttleDrag={0}
      onDrag={handleDrag}
    />
  );
};

export default App
Read more comments on GitHub >

github_iconTop Results From Across the Web

Create React App not working - Stack Overflow
1. update the npm version (npm install npm@latest -g) · 2. clear the cache (npm cache clean --force) · 3. create the react...
Read more >
Versioning Policy - React
Alpha and canary versions of React.​​ We provide alpha versions of React as a way to test new features early, but we need...
Read more >
Fixing the 'cannot GET /URL' error on refresh with React ...
The first, set up both client and server side routing. The second, redirect all server requests to /index.html which will download all the...
Read more >
Troubleshooting | React Navigation
Before troubleshooting an issue, make sure that you have upgraded to the latest available versions of the packages. You can install the latest...
Read more >
How To Set Up a React Project with Create React App
If you stopped the server, be sure to restart it with npm start . Now, take some time to see the parts of...
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