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
2 Answers
Reset to default 4Wrap 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条)