This hook save last emitted event data in state and return state.
If You want use hooks, hocs or component decorator You must use EventrixProvider in Your application. Go to "EventrixProvider" page in "Context" section for more details.
Javascript Typescript
Copy import React from 'react' ;
import { useEventState , useEmit } from 'eventrix' ;
const UndoDeleteUserButton = () => {
const [ eventState , setEventState ] = useEventState ( 'removeUser' );
const emit = useEmit ();
const addUser = useCallback (() => {
emit ( 'addUser' , eventState);
setEventState ( null );
} , [emit , eventState , setEventState]);
if ( ! eventState) {
return null ;
}
return (
< button onClick = {addUser}>
Undo user delete
</ button >
);
}
Copy import React from 'react' ;
import { useEventState , useEmit } from 'eventrix' ;
import { User } from './interfaces' ;
const UndoDeleteUserButton = () => {
const [ eventState , setEventState ] = useEventState < User | undefined | null >( 'removeUser' );
const emit = useEmit < User >();
const addUser = useCallback (() => {
if (eventState) {
emit ( 'addUser' , eventState);
setEventState ( null );
}
} , [emit , eventState , setEventState]);
if ( ! eventState) {
return null ;
}
return (
< button onClick = {addUser} >
Undo user delete
</ button >
);
}