Skip to content

Commit

Permalink
Fix initialValue not set
Browse files Browse the repository at this point in the history
But following warning is now shown in console:

"""
Warning: A component is changing a controlled input to be uncontrolled.
This is likely caused by the value changing from a defined to undefined, which should not happen.
Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/link/controlled-components
"""
  • Loading branch information
ekzyis committed Feb 1, 2024
1 parent f987a94 commit c9968b9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
14 changes: 14 additions & 0 deletions components/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,3 +961,17 @@ export function DatePicker ({ fromName, toName, noForm, onChange, when, from, to
/>
)
}

export function ClientInput ({ initialValue, ...props }) {
// This component can be used for Formik fields
// where the initial value is not available on first render.
// Example: value is stored in localStorage which is fetched
// after first render using an useEffect hook.
const [,, helpers] = useField(props)

useEffect(() => {
helpers.setValue(initialValue)
}, [initialValue])

return <Input {...props} />
}
8 changes: 5 additions & 3 deletions pages/settings/wallets/lnbits.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getGetServerSideProps } from '../../../api/ssrApollo'
import { Form, Input } from '../../../components/form'
import { Form, ClientInput } from '../../../components/form'
import { CenterLayout } from '../../../components/layout'
import { WalletButtonBar, WalletCard } from '../../../components/wallet-card'
import { lnbitsSchema } from '../../../lib/validate'
Expand Down Expand Up @@ -35,13 +35,15 @@ export default function LNbits () {
}
}}
>
<Input
<ClientInput
initialValue={url}
label='lnbits url'
name='url'
required
autoFocus
/>
<Input
<ClientInput
initialValue={adminKey}
type='password'
autoComplete='false'
label='admin key'
Expand Down
5 changes: 3 additions & 2 deletions pages/settings/wallets/nwc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getGetServerSideProps } from '../../../api/ssrApollo'
import { Form, Input } from '../../../components/form'
import { Form, ClientInput } from '../../../components/form'
import { CenterLayout } from '../../../components/layout'
import { WalletButtonBar, WalletCard } from '../../../components/wallet-card'
import { nwcSchema } from '../../../lib/validate'
Expand Down Expand Up @@ -34,7 +34,8 @@ export default function NWC () {
}
}}
>
<Input
<ClientInput
initialValue={nwcUrl}
label='connection'
name='nwcUrl'
required
Expand Down

0 comments on commit c9968b9

Please sign in to comment.