首页 > 其他分享 >React的onChange事件支持冒泡

React的onChange事件支持冒泡

时间:2023-09-23 10:33:19浏览次数:30  
标签:React radio 冒泡 挂载 onChange change 事件

React的合成事件,所有事件都冒泡到document,带来的一个方便的地方就是,原本原生事件不支持冒泡的,在React中都支持冒泡

例如 focus,blur,change,submit,reset,select 等事件不支持冒泡,

但是在 React中,可以使用同名的合成事件来支持冒泡,这样可以带来一个好处,减少事件挂载


例如 对 radio 组挂载change事件

在原生 html 中,应该是这样

<div>
    <label className="rd"><input type="radio" value="1" name="gender" onchange="getValue(this)" /> Male </label>
    <label className="rd"><input type="radio" value="2" name="gender" onchange="getValue(this)" /> Female </label>
    <label className="rd"><input type="radio" value="3" name="gender" onchange="getValue(this)" /> Other </label>
</div>

要挂载多次onchange事件

当然,使用js或jquery可能会省点事

//js必须使用forEach,不能直接对集合绑定事件
var radios = document.querySelectorAll('input[type=radio][name="gender"]');
radios.forEach(radio => radio.addEventListener('change', () => alert(radio.value)));

//jquery可以对集合绑定事件
$('input[type=radio][name="gender"]').on('change', function() {
    alert(this.value);
});

而在React中,可以直接在父元素上挂载onChange事件,简化代码

<div onChange={this.onChangeValue}>
    <label className="rd"><input type="radio" value="1" name="gender" /> Male </label>
    <label className="rd"><input type="radio" value="2" name="gender" /> Female </label>
    <label className="rd"><input type="radio" value="3" name="gender" /> Other </label>
</div>

此思路可以扩展,用以处理其他问题,待补充

标签:React,radio,冒泡,挂载,onChange,change,事件
From: https://www.cnblogs.com/mengff/p/17723967.html

相关文章

  • react native 使用 KeyboardAvoidingView 无效
    组件介绍:该组件将根据键盘高度自动调整其高度、位置或底部填充,以在显示虚拟键盘时保持可见。官方文档:KeyboardAvoidingView文档地址遇到的问题:KeyboardAvoidingView标签要设置behavior={Platform.OS==="ios"?"padding":"height"},iOS系统要使用padding否则样式可能......
  • react的todolist
    React的todolistsrc/main.jsximportReactfrom'react'importReactDOMfrom'react-dom/client'importAppfrom'./App.jsx'import'./index.css'ReactDOM.createRoot(document.getElementById('root')).render(......
  • react的todolist拆分项目
    React的todolist的拆分项目TodoList.jsximportTodofrom"./Todo"exportdefaultfunctionTodoList({todos,toggleTodo,deleteTodo}){return<><ul>{todos.map(todo=><......
  • React学习之类组件的this指向问题
    免责声明我们幼儿园有适合自己看的注释拉满版文档,目标是我奶来都能看懂(不是)。1.前置知识类this指向call、bind、apply待展开...欸嘿,我怎么什么都想不己来了1.1es6类的简单回顾   classPerson{    //构造器    constructor(name,age){ ......
  • react基础操作
    组件之间进行参数传递首先我们创建一个组件,在我们的主程序中把数据传递过去import{useState}from'react'importSOMEfrom'./g6/ant-d-g6'import'./App.css'functionApp(){const[data,setData]=useState<String>('传递参数')return(......
  • vue3的ref、reactive的使用
    一、介绍ref和reactive是Vue3中用来实现数据响应式的API,一般情况下,ref推荐定义基本数据类型,reactive推荐定义引用数据类型(对象或数组) 二、ref与reactive对比<template><p>{{person.name}}</p><p>{{person.long}}</p><p>{{age}}</p><p>{{info.addr......
  • React 虚拟滚动 长列表
    定高版本1"useclient";2importReact,{useCallback,useMemo,useState}from"react";34interfaceIProps{5list:any[];6fixedHeight:number;7}89//Fixedheight10constVirtualView=(props:IProps)=>{1......
  • 一键实现冒泡排序算法,代码质量有保障!
    近年来,深度学习和神经语言模型作为提高开发人员生产力的手段,尤其是2022年11月30日,ChatGPT这一现象级热点得出横空出世,在全球范围内形成了热烈的讨论,其中关于自动化代码生成和其它软件工程方面受到了极大的关注。 软件开发过程涵盖了各种代码生成任务,包括代码自动生成、代码翻......
  • 一键实现冒泡排序算法,代码质量有保障!
    近年来,深度学习和神经语言模型作为提高开发人员生产力的手段,尤其是2022年11月30日,ChatGPT这一现象级热点得出横空出世,在全球范围内形成了热烈的讨论,其中关于自动化代码生成和其它软件工程方面受到了极大的关注。软件开发过程涵盖了各种代码生成任务,包括代码自动生成、代码翻译和......
  • 冒泡排序
    importjava.util.Arrays;publicclassarrayDemo7{publicstaticvoidmain(String[]args){int[]arrays={5,2,3,1,4,6};sortArrays(arrays);System.out.println(Arrays.toString(arrays));}publicstaticint[]sortArrays(int[]arrays){for(intlength=ar......