๐ŸŒ
npm
npmjs.com โ€บ package โ€บ react-modal-hook
react-modal-hook - npm
July 22, 2022 - React hook for showing modal windows. Latest version: 3.0.2, last published: 4 years ago. Start using react-modal-hook in your project by running `npm i react-modal-hook`. There are 16 other projects in the npm registry using react-modal-hook.
      ยป npm install react-modal-hook
    
Published ย  Jul 22, 2022
Version ย  3.0.2
Author ย  mpontus
๐ŸŒ
Medium
medium.com โ€บ codex โ€บ 3-simple-custom-react-hooks-for-mastering-modal-management-cc9a6fc24937
3 Simple Custom React Hooks for Mastering Modal Management | by Andrew Richard | CodeX | Medium
May 18, 2024 - In this article, we will dive into three custom React hooks โ€” `useModalManager`, `useClickOutside`, and `useModal` โ€” that epitomize the hook philosophy. These tools encapsulate intricate behaviors in reusable, composable functions, allowing you to integrate sophisticated UI management into your projects with unparalleled ease. Effortlessly Juggle Multiple Modals with `useModalManager`
๐ŸŒ
GitHub
github.com โ€บ alexanderkhivrych โ€บ use-modal-hook
GitHub - alexanderkhivrych/use-modal-hook: ๐Ÿš€ React hook for controlling modal components
import React, { memo } from "react"; import { useModal, ModalProvider } from "use-modal-hook"; import Modal from "react-modal"; // It can be any modal const MyModal = memo( ({ isOpen, onClose, title, description, closeBtnLabel }) => ( <Modal isOpen={isOpen} onRequestClose={onClose}> <h2>{title}</h2> <div>{description}</div> <button onClick={onClose}>{closeBtnLabel}</button> </Modal> ) ); const SomePage = memo(() => { const [showModal, hideModal] = useModal(MyModal, { title: "My Test Modal", description: "I Like React Hooks", closeBtnLabel: "Close" }); return ( <> <h1>Test Page</h1> <button onClick={showModal}>Show Modal</button> </> ); }); const App = () => ( <ModalProvider> <SomePage /> </ModalProvider> );
Starred by 69 users
Forked by 4 users
Languages ย  JavaScript 100.0% | JavaScript 100.0%
๐ŸŒ
GitHub
github.com โ€บ mpontus โ€บ react-modal-hook
GitHub - mpontus/react-modal-hook: Syntactic sugar for handling modals using React Hooks
Syntactic sugar for handling modal windows using React Hooks.
Starred by 254 users
Forked by 30 users
Languages ย  TypeScript 89.6% | JavaScript 6.4% | HTML 2.7% | CSS 1.3% | TypeScript 89.6% | JavaScript 6.4% | HTML 2.7% | CSS 1.3%
๐ŸŒ
GitHub
github.com โ€บ microcmsio โ€บ react-hooks-use-modal
GitHub - microcmsio/react-hooks-use-modal: This is a customizable modal with react-portal.
import React, { useState, useCallback } from 'react'; import { createRoot } from 'react-dom/client'; import { useModal } from 'react-hooks-use-modal'; const App = () => { const [Modal, open, close, isOpen] = useModal('root', { preventScroll: true, focusTrapOptions: { clickOutsideDeactivates: false, }, }); return ( <div> <p>Modal is Open?
Starred by 171 users
Forked by 22 users
Languages ย  TypeScript 80.2% | JavaScript 12.1% | CSS 7.2% | Shell 0.5% | TypeScript 80.2% | JavaScript 12.1% | CSS 7.2% | Shell 0.5%
๐ŸŒ
Upmostly
upmostly.com โ€บ home โ€บ tutorials โ€บ modal components in react using custom hooks
How to Use Modal Components in React with Custom Hooks - Upmostly
October 28, 2021 - A Hook in React is a function that shares common logic between multiple components. For example, showing and hiding a react modal component.
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ react-simple-hook-modal
react-simple-hook-modal - npm
October 30, 2020 - A simple React modal with hook based API. Latest version: 1.1.0, last published: 5 years ago. Start using react-simple-hook-modal in your project by running `npm i react-simple-hook-modal`. There are 4 other projects in the npm registry using react-simple-hook-modal.
      ยป npm install react-simple-hook-modal
    
Published ย  Oct 30, 2020
Version ย  1.1.0
Author ย  Matthew Brookson
๐ŸŒ
CodeSandbox
codesandbox.io โ€บ s โ€บ react-modal-component-with-custom-hooks-3xyhc
React modal component with custom hooks - CodeSandbox
April 13, 2020 - React modal component with custom hooks by alioukahere using react, react-dom, react-scripts, styled-jsx
Published ย  Apr 13, 2020
Author ย  alioukahere
Find elsewhere
๐ŸŒ
DEV Community
dev.to โ€บ viclafouch โ€บ build-a-complete-modal-component-with-react-hooks-2fk8
๐Ÿ”ง Build a complete Modal Component with React Hooks ๐ŸŒˆ - DEV Community
July 9, 2020 - Secondly, what we want is a modal that is independent of our application. We don't want to have any z-index concerns in css or any overflow concerns. So let's insert this component into a different location in the DOM. But how? Portals provide a way to render children in a DOM node that exists outside of the DOM component hierarchy, that is, outside of the #root or #app div of your React application.
๐ŸŒ
w3collective
w3collective.com โ€บ category: tutorial โ€บ building a react modal component using a custom hook
Building a React modal component using a custom Hook - w3collective
February 15, 2023 - Custom Hooks should always start with use so that you can quickly tell that itโ€™s a re-usable Hook: import { useState } from 'react'; const useModal = () => { const [visible, setVisible] = useState(false); function toggle() { setVisible(!visible); } return {toggle, visible} }; export default useModal;Code language: JavaScript (javascript) Also in the /src folder create a new file called Modal.js for the modal component itself: import React from "react"; import ReactDOM from "react-dom"; const Modal = ({ visible, toggle }) => visible ?
Top answer
1 of 2
2

You need to add a function in the app.js to handle the click on the button, so the function will change the state show to true, and you will pass the state to your modal, like this:

App.js

[showState, setShowState] = useState(false)
buttonClickedHandler = () => {
   setShowState((showState) => showState = !showState )
}

<div className="App">
      <HeroSlider buttonClicked={buttonClickedHandler} />
      <HowItWorks />
      <Modal show={showState} buttonClicked={buttonClickedHandler} />
      <Footer />
</div>

HeroSlider.js

import React, { useState } from "react";
import Header from './Header'

function HeroSlider(props) {
    const [show, setShow] = useState(false);
    const modalShow = () => setShow(true);
    const openIsAccount = () => {

    }
    return (

 <div className="jumbotron" id="jumbotron2" >
<button type="button" className="btn btn-primary" id="shoutBtn" onClick={props.buttonClicked}><span>Get Started</span>
</button>
 </div>
    );
}

export default HeroSlider;

IsAccountOpen.js

const IsAccountOpen = (props) => {
    const [show, setShow] = useState(false);
    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);

    return (
        <>
            <Modal show={props.show} onHide={props.buttonClicked} backdrop="static" keyboard={false}>
                <Modal.Header closeButton>
                    <Modal.Title>Modal title</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    I will not close 
                 </Modal.Body>
            </Modal>
        </>
    );

};

export default IsAccountOpen

I think the best solution is to use redux, because you will need to access and update the state from different components, you shouldn't use context API because the state changes frequently and you will hit the performance.

2 of 2
0

You'll need to extract the state to App level, so that this can be shared to components. You can do this with useState, or using context & custom hooks if you want to make it very clean. But do it with useState on first.

HeroSlider should receive modalShow function in it's props. Modal.js should receive the show state as props.

๐ŸŒ
Reactgo
reactgo.com โ€บ home โ€บ react modal tutorial using hooks
React Modal tutorial using hooks | Reactgo
August 19, 2023 - The useState hook returns an array with two properties which are show and setShow. setShow is a function which is used to update the state. The closeModal and openModal functions are used to opening and closing the modal.
๐ŸŒ
Better Programming
betterprogramming.pub โ€บ create-a-custom-usemodal-react-hook-449b5909cc09
Create a Custom useModal() React Hook | by Oyetoke Tobiloba Emmanuel | Better Programming
August 15, 2019 - Declare a setModalState function that takes in state as a prop, call setModalOpen to control the modal and set selected to null if state is false. Then we return the things that can be accessed out of the Hooks. For this example, we are going to create three components: UserTable: Displays a list of users in a table. UserDetail: Shows full details of a user. UserInformation: A component wrapper for both UserTable and UserDetail. ... What we have above is a simple React component which maps through given data and presents it in a table.
๐ŸŒ
DEV Community
dev.to โ€บ alexandprivate โ€บ your-next-react-modal-with-your-own-usemodal-hook-context-api-3jg7
Your next React Modal with your own "useModal" Hook & Context API. - DEV Community
November 9, 2020 - Hi there everyone, this is a quick review about how to use Modals Components in your React project combining Hooks, Context, and Portals. You need to have some experience coding with React, and be aware of React's latest updates like Hooks and Context API.
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ react-hooks-use-modal
react-hooks-use-modal - npm
January 24, 2023 - A react hook which can open the modal with react-portal. Latest version: 3.3.1, last published: 3 years ago. Start using react-hooks-use-modal in your project by running `npm i react-hooks-use-modal`. There are no other projects in the npm registry ...
      ยป npm install react-hooks-use-modal
    
Published ย  Jan 24, 2023
Version ย  3.3.1
Author ย  shibe97
๐ŸŒ
DEV Community
dev.to โ€บ lico โ€บ react-ts-how-i-manage-modal-components-custom-modals-hook-4nmg
React TS: How I manage modal components (Custom Modal Hook) - DEV Community
September 5, 2022 - You can implement the modal like below. I used MUI for UI. import { useForm } from 'react-hook-form'; import Box from '@mui/material/Box'; import Modal from '@mui/material/Modal'; import Grid from '@mui/material/Grid'; import Button from '@mui/material/Button'; import TextField from '@mui/material/TextField'; import { IModal } from '../../../types/modal'; export interface PostUploadModalProps extends IModal { onSubmit?: (title: string, content: string) => void; } const style = { position: 'absolute' as 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', width: 400, bgcolor
๐ŸŒ
Devdreaming
devdreaming.com โ€บ blogs โ€บ create-efficient-modal-react-portals
Creating an Efficient React Modal Component with Hooks and Portals
October 30, 2023 - To manage the modal's open and close state, let's use the useState hook ๐Ÿ‘‡: import { useState } from "react"; import "./App.scss"; function App() { const [modal, setModal] = useState(false); const Toggle = () => setModal(!modal); return ( ...