reactjs onClick在react js中不支持可拖动图标?

ee7vknir  于 2023-04-05  发布在  React
关注(0)|答案(2)|浏览(113)

我有一个可拖动的图标。但是onClick不能在那个图标上工作。

import styled from "@emotion/styled"
import Draggable from 'react-draggable';
import { BsPlusCircle } from 'react-icons/bs';

const StartWrapper = styled.div`
position: absolute;
bottom: 74px;
display: block;
margin: 0 auto;
width: calc(100% - 24px);
font-family: Montserrat, sans-serif;
font-size: 14px;
font-style: normal;
font-weight: 500;
letter-spacing: 0em;
text-align: center;
color: rgba(91, 91, 91, 0.69);
border: 1px solid white;
height: 170px;
line-height: 170px;
`

export const App = ( ) => {

    const show = () => {
        console.log("Show");
    }

    return (
         <StartWrapper onClick={()=> start()}>
                <Draggable>       
                      <BsPlusCircle  onClick={() => show()}  />
                </Draggable>
         </StartWrapper>
    )
}

开始功能起作用。但是显示功能不起作用。任何帮助都将不胜感激。

fzsnzjdm

fzsnzjdm1#

看起来您需要向StartWrapper和Draggable组件传递一个引用

import styled from "@emotion/styled";
import { useRef } from "react";
import Draggable from "react-draggable";
import { BsPlusCircle } from "react-icons/bs";

const StartWrapper = styled.div`
  position: absolute;
  bottom: 74px;
  display: block;
  margin: 0 auto;
  width: calc(100% - 24px);
  font-family: Montserrat, sans-serif;
  font-size: 14px;
  font-style: normal;
  font-weight: 500;
  letter-spacing: 0em;
  text-align: center;
  color: rgba(91, 91, 91, 0.69);
  border: 1px solid white;
  height: 170px;
  line-height: 170px;
`;

export const App = () => {
  const ref = useRef(null); //ADD THIS
  const show = () => {
    console.log("Show");
  };

  return (
//Then pass refs to StartWrapper and Draggable
    <StartWrapper ref={ref} onClick={() => console.log("Hello")}>
      <Draggable ref={ref}>
        <BsPlusCircle onClick={() => show()} />
      </Draggable>
    </StartWrapper>
  );
};

这在codesanbox中对我有效
findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of DraggableCore which is inside StrictMode

whlutmcx

whlutmcx2#

<Draggable cancel=".clickable">
    <div className="clickable" onClick={() => callFunc()}></div>
</Draggable>

相关问题