Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yunho1017/react-custom-event

Define your own custom events in react !
https://github.com/yunho1017/react-custom-event

Last synced: 5 days ago
JSON representation

Define your own custom events in react !

Awesome Lists containing this project

README

        

# react-custom-event
Define your own custom events in react !

## Introduction

Let's implement a dialog using React.

The dialog has inputs. if values ​​are entered in the inputs, leave alert should be displayed when closing the dialog.

In this case, How would you implement this?

There are several ways to solve this. You can use ref or manage the values ​​on the side that declares the dialog.

How about this?

```js

const DialogCloseEvent = createCustomEvent("dialog-close");

const Dialog = (props) => {
return {
try {
DialogCloseEvent.call()
props.onClose()
}
}>
{props.children}

}

const Component = () => {
const [state, setState] = useState()
useEffect(() => {
const handler = () => {
if (state) {
throw new Error("leaveAlert!")
openLeaveAlert()
}
}

DialogCloseEvent.addEventListener(handler)
return () => {
DialogCloseEvent.removeEventListener(handler)
}
},[])
return
}

```