tangway / flag-guessing-game-frontend

0 stars 0 forks source link

make Modal for hints and bonus questions #15

Open tangway opened 5 months ago

tangway commented 5 months ago
tangway commented 5 months ago

make this component modular so that it can be reusable for hints and bonus qns and also if it doesnt work out for the flow of the game it can be removed with more ease

tangway commented 4 months ago
tangway commented 4 months ago

to use the .showModal() and .close() methods from the HTMLDialogElement interface you have to manipulate the DOM but this does not fit the React paradigm as you risk introducing inconsistencies between the virtual DOM (the representation of the page stored in memory managed by React) and the real DOM (what's actually displayed in the browser window). These inconsistencies can cause unexpected behaviors, bugs, and poor performance.

a suggested workaround is to use useRef to manipulate the DOM as shown here: https://medium.com/@bomber.marek/how-to-use-dialog-in-react-easy-modals-tooltips-81e44d570c8a

tangway commented 4 months ago

in the article linked above it also describes how using React.createPortal is more cumbersome than using the dialog element:


ReactDOM.createPortal vs dialog

Before the

element, creating modals in React was a bit more complex. The most common and correct way was to use ReactDOM.createPortal.

This API allows you to render children into a DOM node that exists outside the DOM hierarchy of the parent component.

In the context of modals, ReactDOM.createPortal is used to append the modal component directly to the body element or another container outside the main React application. Doing this avoids CSS stacking context issues and makes the modal appear on top of other elements.

However, using ReactDOM.createPortal requires you to manage the state of the modal (whether it's open or closed) and handle the appending and removing of the modal component from the DOM. This adds unnecessary complexity and might cause additional re-renders.

On the other hand, the

element simplifies this process since it’s a native HTML solution that doesn’t require any additional state management or DOM manipulations.