useModal - React custom hook
Hook to wrap a component and show it as a modal.
It returns:
- Wrapped component as a new component.
- Status as a boolean.
- Toggle function.
Why useModal?
With this hook, you can easily render components showing them as a modal:
- View components as a modals.
- Share logic to the modal component.
- Call toggle function to open/close the modal.
Usage
Just install:
npm install react-hook-usemodal
And import the hook:
;
Use it in your component:
; const YourComponent = { ... const Modal show toggle = ; return <Modal /> ...}
Optional, you can share logic as a params:
return <Modal prop1='string' prop2= console />
Full Example
(Check it in deployed example)
Component to show as modal:
; return <div className='form-example'> <h1>Item component</h1> <input placeholder="Username.." /> <button>Add</button> <br /> <button onClick=propscloseModal>cancel</button> </div>
Main component, where you call the modal:
; ;; { const Modal show toggle = return <div className="App"> show && <Modal closeModal=toggle /> <button onClick= >Add user</button> </div> ;} ;