Skip to content

Commit 3519cce

Browse files
crutchcornKevinVandyautofix-ci[bot]
authored
fix: solid fields should not render every field on keystroke anymore (#1959)
* fix: solid fields should not render every field on keystroke anymore Co-authored-by: Kevin Van Cott <[email protected]> * ci: apply automated fixes and generate docs * chore: fix lint --------- Co-authored-by: Kevin Van Cott <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent c61349e commit 3519cce

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

.changeset/puny-wombats-tap.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@tanstack/solid-form': patch
3+
'@tanstack/form-core': patch
4+
---
5+
6+
Fix double-rendering of Solid fields

packages/form-core/src/FieldApi.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,33 @@ export class FieldApi<
11691169

11701170
this.store = new Derived({
11711171
deps: [this.form.store],
1172-
fn: () => {
1172+
fn: ({ prevVal: _prevVal }) => {
1173+
const prevVal = _prevVal as
1174+
| FieldState<
1175+
TParentData,
1176+
TName,
1177+
TData,
1178+
TOnMount,
1179+
TOnChange,
1180+
TOnChangeAsync,
1181+
TOnBlur,
1182+
TOnBlurAsync,
1183+
TOnSubmit,
1184+
TOnSubmitAsync,
1185+
TOnDynamic,
1186+
TOnDynamicAsync,
1187+
TFormOnMount,
1188+
TFormOnChange,
1189+
TFormOnChangeAsync,
1190+
TFormOnBlur,
1191+
TFormOnBlurAsync,
1192+
TFormOnSubmit,
1193+
TFormOnSubmitAsync,
1194+
TFormOnDynamic,
1195+
TFormOnDynamicAsync
1196+
>
1197+
| undefined
1198+
11731199
const meta = this.form.getFieldMeta(this.name) ?? {
11741200
...defaultFieldMeta,
11751201
...opts.defaultMeta,
@@ -1185,6 +1211,10 @@ export class FieldApi<
11851211
value = this.options.defaultValue
11861212
}
11871213

1214+
if (prevVal && prevVal.value === value && prevVal.meta === meta) {
1215+
return prevVal
1216+
}
1217+
11881218
return {
11891219
value,
11901220
meta,

packages/solid-form/src/createField.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
onCleanup,
77
onMount,
88
} from 'solid-js'
9+
import { useStore } from '@tanstack/solid-store'
910
import type {
1011
DeepKeys,
1112
DeepValue,
@@ -251,8 +252,14 @@ function makeFieldReactive<
251252
TParentSubmitMeta
252253
> {
253254
const [field, setField] = createSignal(fieldApi, { equals: false })
254-
const unsubscribeStore = fieldApi.store.subscribe(() => setField(fieldApi))
255-
onCleanup(unsubscribeStore)
255+
// Handle shallow comparison to make sure that Derived doesn't create a new setField call every time
256+
const store = useStore(fieldApi.store, (store) => store)
257+
// Run before initial render
258+
createComputed(() => {
259+
// Use the store to track dependencies
260+
store()
261+
setField(fieldApi)
262+
})
256263
return field
257264
}
258265

0 commit comments

Comments
 (0)