You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Take Material UI's Dialog component as an example that has open: boolean React prop as a way to manage its open/closed state. In Material UI documentation you will find a usage example similar to this:
If the dialog can be used without a need to manage its state in-place that code would look nice and clean.
There are multiple ways to implement it, e.g. by introducing a top-level DialogProvider component + useDialog(...) React hook, alternatively you can add an imperative handler to the dialog itself so that it can be opened using dialogRef.current?.open() method available on the dialog instance.
Now let's see how the implementation of this dialog including .open() method implemented with useImeprativeHandle(ref, ...), useState() React hooks looks like:
There are pros and cons of this approach, on the good side is that it's fully self-contained and doesn't rely on any external state management solutions.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Take Material UI's
Dialog
component as an example that hasopen: boolean
React prop as a way to manage its open/closed state. In Material UI documentation you will find a usage example similar to this:In the original example, the dialog is used in place. Normally, you want to extract dialog in a standalone component, for example:
Afterwards, the original example could be reduced to the following:
If the dialog can be used without a need to manage its state in-place that code would look nice and clean.
There are multiple ways to implement it, e.g. by introducing a top-level
DialogProvider
component +useDialog(...)
React hook, alternatively you can add an imperative handler to the dialog itself so that it can be opened usingdialogRef.current?.open()
method available on the dialog instance.Now let's see how the implementation of this dialog including
.open()
method implemented withuseImeprativeHandle(ref, ...)
,useState()
React hooks looks like:There are pros and cons of this approach, on the good side is that it's fully self-contained and doesn't rely on any external state management solutions.
Beta Was this translation helpful? Give feedback.
All reactions