React使用jQuery调用resizable报错,如何实现可拖拽可调整大小的模态框
问题原因
resizable()、draggable()不属于jQuery原生方法,是jQuery UI库提供的交互能力,你仅引入了jQuery,未引入jQuery UI及对应样式,因此调用不存在的方法抛出报错。- 你在
constructor生命周期中操作DOM,此时组件尚未挂载到页面,.modal-content、.modal-dialog等节点还不存在,即使引入jQuery UI也无法选中元素完成绑定。 - 你当前代码未初始化
state.showModal,直接调用this.setState操作未定义的状态属性,也会触发运行时报错。
方案1:基于jQuery的修正实现
操作步骤
- 安装完整依赖:执行
npm install jquery jquery-ui-dist - 组件顶部引入依赖和对应样式:
import $ from 'jquery' import 'jquery-ui-dist/jquery-ui.css' import 'jquery-ui-dist/jquery-ui'
- 初始化状态,将DOM操作移到
componentDidMount生命周期执行,组件卸载时销毁绑定的交互实例和事件避免内存泄漏。
修正后代码片段
import React from 'react'; import { Modal, ModalHeader, ModalBody } from 'reactstrap'; import $ from 'jquery'; import 'jquery-ui-dist/jquery-ui.css'; import 'jquery-ui-dist/jquery-ui'; export default class RND extends React.Component { constructor() { super(); // 初始化状态 this.state = { showModal: true }; } componentDidMount() { // 组件挂载后再操作DOM $('.modal-content').resizable({ minHeight: 300, minWidth: 300 }); $('.modal-dialog').draggable(); $('#myModal').on('show.bs.modal', function() { $(this) .find('.modal-body') .css({ 'max-height': '100%' }); }); } componentWillUnmount() { // 销毁实例解绑事件 $('.modal-content').resizable('destroy'); $('.modal-dialog').draggable('destroy'); $('#myModal').off('show.bs.modal'); } modalClose = () => { this.setState({ showModal: false }); }; render() { return ( <Modal id="myModal" isOpen={this.state.showModal} toggle={this.modalClose} style={{ minWidth: '30rem', border: '0.1rem solid green' }} > <ModalHeader className="modal-header bg-primary modal-title text-white" toggle={this.modalClose} > <h2> Header </h2> </ModalHeader> <ModalBody> <div className="form-group row"> <div className="FormRow col-sm-12"> <span>Body data </span> </div> </div> </ModalBody> </Modal> ); } }
方案2:无jQuery纯React实现(更推荐)
React项目中混用jQuery操作DOM容易导致状态和视图不同步,推荐使用专门的React拖拽缩放库react-rnd实现需求,不需要依赖jQuery。
操作步骤
- 安装依赖:执行
npm install react-rnd - 用
Rnd组件包裹模态框内容,直接配置拖拽、缩放参数即可。
实现代码
import React from 'react'; import { Modal, ModalHeader, ModalBody } from 'reactstrap'; import { Rnd } from 'react-rnd'; export default class RND extends React.Component { constructor() { super(); this.state = { showModal: true, // 初始化模态框宽高和位置 width: 480, height: 300, x: 100, y: 100 }; } modalClose = () => { this.setState({ showModal: false }); }; render() { return ( <Modal isOpen={this.state.showModal} toggle={this.modalClose} // 关闭模态框默认的位置样式,交给Rnd控制 modalClassName="d-block position-static" contentClassName="border-0 shadow-none" > <Rnd size={{ width: this.state.width, height: this.state.height }} position={{ x: this.state.x, y: this.state.y }} onDragStop={(e, d) => { this.setState({ x: d.x, y: d.y }); }} onResizeStop={(e, direction, ref, delta, position) => { this.setState({ width: ref.offsetWidth, height: ref.offsetHeight, ...position }); }} minWidth={300} minHeight={300} // 只允许拖拽头部移动 dragHandleClassName="modal-header" style={{ border: '0.1rem solid green', background: '#fff' }} > <ModalHeader className="modal-header bg-primary modal-title text-white" toggle={this.modalClose} > <h2> Header </h2> </ModalHeader> <ModalBody> <div className="form-group row"> <div className="FormRow col-sm-12"> <span>Body data </span> </div> </div> </ModalBody> </Rnd> </Modal> ); } }
内容的提问来源于stack exchange,提问作者jjzjx118_2
相关产品推荐
相关产品推荐

