fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

[react] 怎样在react中创建一个事件?

Open wwwind213 opened this issue 6 years ago • 2 comments

[react] 怎样在react中创建一个事件?

wwwind213 avatar Jul 13 '19 16:07 wwwind213

var EventEmitter = require('events').EventEmitter; class App extends Component{ constructor(props){ super(props); } componentDidMount(){ this.itemChange = emitter.addListener('ItemChange',(msg,data)=>console.log(msg));//注册事件 } componentWillUnmount(){ emitter.removeListener(this.itemChange);//取消事件 } render(){ return ( <List list={[{text:1},{text:2}]}/> ) } }

zhaofeipeter avatar Aug 06 '20 06:08 zhaofeipeter

// hooks写法 import React, {useState} from 'react'

const index = () => { const [state, setState] = useState(false) const handleClick = ()=>{ setState(state=>!state) } return (

<button onClick={handleClick}>点击
状态:{state}
) }

export default index

WangJia-mm avatar Jun 27 '24 03:06 WangJia-mm