React(5)--事件处理

一个完整的界面,一半是展示,另一半是响应。React 通过将事件处理器绑定到组件上来处理事件(事件触发,更新组件状态,从而重绘)。


让我们看看以下列子

<button class=" sub "  onClick = { this.handleClick } > 确定 </button>

//或者

React.DOM.button( { calssName : "sub", onClick : this.handleClick }, "确定");

这里大家要了解一下:React 写法上类似 HTML 内联事件处理器属性,但底层并没有使用 HTML 的 onClick 属性(通过事件代理之类的手法)。

React 对各类事件提供了很好的支持,大家可以浏览一下 React 的事件系统


只看不写有点无聊,写点无意义的显示,目的:尝试 stateless 和 es6 重构React写法,试验 React事件处理和了解 HTML5 拖动。如果大家觉得我写的不对,请留言指正。下面是 js 部分:

/*
 * @2016年9月27日21:11:48
 * 目的:
 * 尝试 stateless 和 es6 重构React写法
 * 试验 React事件处理
 * 了解 HTML5 拖动
 */

/*Logo模块*/
let Log = ()=>{
    return(
        <div id="log" draggable="true" onDragStart={drag}>A log</div>
    );
}
/*搜索模块*/
let SearchBox = ()=>{
    return(
        <div id="search" draggable="true" onDragStart={drag}>A SearchBox</div>
    );
}
/*新闻模块*/
let NewList = ()=>{
    return(
        <div id="news" draggable="true" onDragStart={drag}>A NewList</div>
    );
}
/*响应onDragStart函数*/
const drag = function(ev)
{
    ev.dataTransfer.setData("Text",ev.target.id);
}


/**
 * 工具条
 * */
class ToolBar extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        return(
            <div>
                {this.props.isLog == true? <Log /> : ""}
                {this.props.isNewList == true? <NewList/> : ""}
                {this.props.isSearchBox == true? <SearchBox/> : ""}
            </div>
        );
    }
}

/*
* 内容界面
* */
class Plant extends React.Component {
    constructor(props) {
        super(props);
        this.state ={
            isLog: false,
            isNewList:false,
            isSearchBox:false,
        };
        this.drop = this.drop.bind(this);
    }

    allowDrop(ev)
    {
        ev.preventDefault();
    }
    drop(ev)
    {
        ev.preventDefault();
        var data=ev.dataTransfer.getData("Text");
        switch (data){
            case 'log' : this.setState({isLog: true});break;
            case 'search' : this.setState({isSearchBox: true});break;
            case 'news' : this.setState({isNewList: true});break;
        }
        ev.target.appendChild(document.getElementById(data));
    }
    render() {
        return(
            <div id="box" style={{border:'1px solid red',width:200,height:300}} onDrop={this.drop} onDragOver={this.allowDrop}>
                <p>{this.state.isLog == true? "添加了 logo 模块" : ""}</p>
                <p>{this.state.isNewList == true? "添加了 新闻 模块" : ""}</p>
                <p>{this.state.isSearchBox == true? "添加了 搜索 模块" : ""}</p>
            </div>
        );
    }
}
//容器
let Content = (tools)=>{
    return(
        <div>
            <ToolBar {...tools}/>
            <Plant/>
        </div>
    );
}
//工具条内容
const tools = {isLog: true, isNewList:true, isSearchBox:true};
ReactDOM.render(
   <Content {...tools}/>,
    document.getElementById('example')
);

html部分:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Hello React!</title>
    <script src="http://static.runoob.com/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.runoob.com/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.runoob.com/assets/react/browser.min.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/babel" src="event.js"></script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React DnD 是一个 React 的拖放库,可以帮助我们快速实现拖放功能。下面是一个基本的示例: 首先,安装 `react-dnd` 和 `react-dnd-html5-backend`: ``` npm install react-dnd react-dnd-html5-backend ``` 然后,创建一个拖放组件: ```jsx import React from 'react'; import { useDrag } from 'react-dnd'; const Box = ({ name }) => { const [{ isDragging }, drag] = useDrag({ item: { name, type: 'box' }, collect: (monitor) => ({ isDragging: !!monitor.isDragging(), }), }); return ( <div ref={drag} style={{ opacity: isDragging ? 0.5 : 1, fontSize: 20, fontWeight: 'bold', cursor: 'move', }} > {name} </div> ); }; export default Box; ``` 在上面的代码中,我们使用 `useDrag` 钩子来创建一个拖动源。`item` 属性指定了拖动源的参数,`collect` 函数用于收集拖动源的状态。 接下来,创建一个拖放目标组件: ```jsx import React from 'react'; import { useDrop } from 'react-dnd'; const Target = ({ onDrop }) => { const [{ isOver }, drop] = useDrop({ accept: 'box', drop: (item, monitor) => { onDrop(item); }, collect: (monitor) => ({ isOver: !!monitor.isOver(), }), }); return ( <div ref={drop} style={{ width: 200, height: 200, backgroundColor: isOver ? 'yellow' : 'white', }} /> ); }; export default Target; ``` 在上面的代码中,我们使用 `useDrop` 钩子来创建一个拖放目标。`accept` 属性指定了接受的拖动源类型,`drop` 函数用于处理拖放事件,`collect` 函数用于收集拖放目标的状态。 最后,在父组件中使用这两个组件: ```jsx import React, { useState } from 'react'; import Box from './Box'; import Target from './Target'; const App = () => { const [droppedBox, setDroppedBox] = useState(null); const handleDrop = (item) => { setDroppedBox(item.name); }; return ( <div> <Box name="Box 1" /> <Box name="Box 2" /> <Box name="Box 3" /> <Target onDrop={handleDrop} /> {droppedBox && <p>Dropped box: {droppedBox}</p>} </div> ); }; export default App; ``` 在上面的代码中,我们使用 `useState` 钩子来保存拖放目标接受的拖动源名称。`handleDrop` 函数用于处理拖放事件,并更新状态。最后,我们在页面上显示拖放的结果。 这就是一个简单的 React DnD 示例。如果想要了解更多内容,可以查看官方文档:https://react-dnd.github.io/react-dnd/docs/overview。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值