Assumming you are internally using a Dialog component from a UI library (like MUI), the second approach is preferred, since it gives the component the responsibility to handle its closing behaviour (like closing transition).
The first approach completely unmount the modal when the state changes, so you may notice that in case the modal has a transition effect on close, it will be completely skipped.
Performance wise, you may assume the second is one is better, but probably not the proper thing to do.
Regarding the undefined issue you are mentioning, you are probably trying to show data that has not been loaded yet, you may want to add a condition to avoid doing so white it's still loading.