Fix useMemo returns new component on change (#2315)
This fixes setting the input value on scan if the component remounts while the scanner is open. No need to use useRef to close the scanner on remount.
This commit is contained in:
parent
243b094fcd
commit
3f74279f29
@ -1350,21 +1350,11 @@ function PasswordScanner ({ onScan, text }) {
|
||||
loading: () => <PageLoading />
|
||||
})
|
||||
|
||||
const closeRef = useRef()
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
// close modal on remount to avoid calling stale callbacks
|
||||
// see https://github.com/stackernews/stacker.news/pull/2313
|
||||
closeRef.current?.()
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<InputGroup.Text
|
||||
style={{ cursor: 'pointer' }}
|
||||
onClick={() => {
|
||||
showModal(onClose => {
|
||||
closeRef.current = onClose
|
||||
return (
|
||||
<div>
|
||||
{text && <h5 className='line-height-md mb-4 text-center'>{text}</h5>}
|
||||
|
@ -49,7 +49,7 @@ export default function Wallet () {
|
||||
? (
|
||||
<WalletLayout>
|
||||
<div className='py-5 d-flex flex-column align-items-center justify-content-center flex-grow-1 mx-auto' style={{ maxWidth: '500px' }}>
|
||||
<PassphrasePrompt />
|
||||
{PassphrasePrompt}
|
||||
</div>
|
||||
</WalletLayout>
|
||||
)
|
||||
|
@ -238,42 +238,41 @@ export function usePassphrasePrompt () {
|
||||
const [showPassphrasePrompt, setShowPassphrasePrompt] = useState(false)
|
||||
const togglePassphrasePrompt = useCallback(() => setShowPassphrasePrompt(v => !v), [])
|
||||
|
||||
const Prompt = useMemo(() =>
|
||||
() => (
|
||||
<div>
|
||||
<h4>Wallet decryption</h4>
|
||||
<p className='line-height-md mt-3'>
|
||||
Your wallets have been encrypted on another device. Enter your passphrase to use your wallets on this device.
|
||||
</p>
|
||||
<p className='line-height-md'>
|
||||
{showPassphrase && 'You can find the button to reveal your passphrase above your wallets on the other device.'}
|
||||
</p>
|
||||
<p className='line-height-md'>
|
||||
Press reset if you lost your passphrase.
|
||||
</p>
|
||||
<Form
|
||||
schema={passphraseSchema({ hash, salt })}
|
||||
initial={{ passphrase: '' }}
|
||||
onSubmit={onSubmit}
|
||||
>
|
||||
<PasswordInput
|
||||
label='passphrase'
|
||||
name='passphrase'
|
||||
placeholder=''
|
||||
required
|
||||
autoFocus
|
||||
qr
|
||||
/>
|
||||
<div className='mt-3'>
|
||||
<div className='d-flex justify-content-between align-items-center'>
|
||||
<Button className='me-auto' variant='danger' onClick={resetPassphrase}>reset</Button>
|
||||
<Button className='me-3 text-muted nav-link fw-bold' variant='link' onClick={togglePassphrasePrompt}>cancel</Button>
|
||||
<SubmitButton variant='primary'>save</SubmitButton>
|
||||
</div>
|
||||
const Prompt = useMemo(() => (
|
||||
<div>
|
||||
<h4>Wallet decryption</h4>
|
||||
<p className='line-height-md mt-3'>
|
||||
Your wallets have been encrypted on another device. Enter your passphrase to use your wallets on this device.
|
||||
</p>
|
||||
<p className='line-height-md'>
|
||||
{showPassphrase && 'You can find the button to reveal your passphrase above your wallets on the other device.'}
|
||||
</p>
|
||||
<p className='line-height-md'>
|
||||
Press reset if you lost your passphrase.
|
||||
</p>
|
||||
<Form
|
||||
schema={passphraseSchema({ hash, salt })}
|
||||
initial={{ passphrase: '' }}
|
||||
onSubmit={onSubmit}
|
||||
>
|
||||
<PasswordInput
|
||||
label='passphrase'
|
||||
name='passphrase'
|
||||
placeholder=''
|
||||
required
|
||||
autoFocus
|
||||
qr
|
||||
/>
|
||||
<div className='mt-3'>
|
||||
<div className='d-flex justify-content-between align-items-center'>
|
||||
<Button className='me-auto' variant='danger' onClick={resetPassphrase}>reset</Button>
|
||||
<Button className='me-3 text-muted nav-link fw-bold' variant='link' onClick={togglePassphrasePrompt}>cancel</Button>
|
||||
<SubmitButton variant='primary'>save</SubmitButton>
|
||||
</div>
|
||||
</Form>
|
||||
</div>
|
||||
), [showPassphrase, resetPassphrase, togglePassphrasePrompt, onSubmit, hash, salt])
|
||||
</div>
|
||||
</Form>
|
||||
</div>
|
||||
), [showPassphrase, resetPassphrase, togglePassphrasePrompt, onSubmit, hash, salt])
|
||||
|
||||
return useMemo(
|
||||
() => [showPassphrasePrompt, togglePassphrasePrompt, Prompt],
|
||||
|
Loading…
x
Reference in New Issue
Block a user