javascript - react-draggable not working with custom components - Stack Overflow

How can I get a custom ponent to drag?The following works when clickedimport React from 'react'

How can I get a custom ponent to drag?

The following works when clicked

import React from 'react';
import { DraggableCore } from 'react-draggable';

export default class Hello extends React.Component {

  onStart = () => {
    console.log('here');
  }

  render() {
    return (
      <DraggableCore
        onStart={this.onStart}
      >
        <h1>Drag me!</h1>
      </DraggableCore>
    );
  }
}

and this does not

import React from 'react';
import { DraggableCore } from 'react-draggable';

class Test extends React.Component {
  render() {
    return (
      <h1>{this.props.children}</h1>
    );
  }
}

export default class Hello extends React.Component {

  onStart = () => {
    console.log('here');
  }

  render() {
    return (
      <DraggableCore
        onStart={this.onStart}
      >
        <Test>Drag me!</Test>
      </DraggableCore>
    );
  }
}

with the only difference being that instead of h1 being used directly, it now uses custom ponent Test

.js

I have tried forwarding the ref. I have also tried wrapping it in a Fragment

https://github./mzabriskie/react-draggable

How can I get a custom ponent to drag?

The following works when clicked

import React from 'react';
import { DraggableCore } from 'react-draggable';

export default class Hello extends React.Component {

  onStart = () => {
    console.log('here');
  }

  render() {
    return (
      <DraggableCore
        onStart={this.onStart}
      >
        <h1>Drag me!</h1>
      </DraggableCore>
    );
  }
}

and this does not

import React from 'react';
import { DraggableCore } from 'react-draggable';

class Test extends React.Component {
  render() {
    return (
      <h1>{this.props.children}</h1>
    );
  }
}

export default class Hello extends React.Component {

  onStart = () => {
    console.log('here');
  }

  render() {
    return (
      <DraggableCore
        onStart={this.onStart}
      >
        <Test>Drag me!</Test>
      </DraggableCore>
    );
  }
}

with the only difference being that instead of h1 being used directly, it now uses custom ponent Test

https://stackblitz./edit/react-ev9iyu?file=Hello.js

I have tried forwarding the ref. I have also tried wrapping it in a Fragment

Share Improve this question asked Feb 11, 2019 at 22:54 tictic 2,5121 gold badge23 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Wrap your ponent with a div inside of DraggableCore:

<DraggableCore onStart={this.onStart}>
  <div>
    <Test>Drag me!</Test>
  </div>
</DraggableCore>

I'm super late to the game here but I hope this helps someone. You have to give your ponent a rest prop. You need to do this because DraggableCore copies the ponent and puts several event listeners on the ponent. Look at the render of DraggableCore. https://github./react-grid-layout/react-draggable/blob/master/lib/DraggableCore.js. Your ponent needs to be able to receive these event listeners.

const Test = ({ children, ...rest }) => {
  return (
    <div {...rest}>{children}</div>
  )
}

const Hello = () => {
  const handleStart = () => console.log('here');

  return (
    <DraggableCore
      onStart={handleStart}
    >
      <Test>Drag me!</Test>
    </DraggableCore>
  );
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744171994a4561581.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信