Skip to content

Commit 74f6959

Browse files
committed
docs(react): add guide for getting and setting field values
1 parent 4e7c8a6 commit 74f6959

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

docs/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@
131131
"label": "Listeners",
132132
"to": "framework/react/guides/listeners"
133133
},
134+
{
135+
"label": "Getting and Setting Values",
136+
"to": "framework/react/guides/getting-and-setting-values"
137+
},
134138
{
135139
"label": "Custom Errors",
136140
"to": "framework/react/guides/custom-errors"
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
id: getting-and-setting-values
3+
title: Getting and Setting Field Values
4+
---
5+
6+
When building forms with TanStack Form, you often need to read or update field values programmatically outside of the standard user input flow. There are several ways to retrieve or change values securely.
7+
8+
## Getting Field Values
9+
10+
There are a few ways to read the value of a field, depending on your context:
11+
12+
### 1. Using `form.getFieldValue`
13+
If you have access to the `form` instance, you can retrieve the current value of any field synchronously using `form.getFieldValue(fieldName)`. This does not trigger a component re-render.
14+
15+
```tsx
16+
const form = useForm({
17+
defaultValues: {
18+
age: 25,
19+
},
20+
})
21+
22+
const checkAge = () => {
23+
const currentAge = form.getFieldValue('age')
24+
console.log('Current age is:', currentAge)
25+
}
26+
```
27+
28+
### 2. Using `form.state.values`
29+
You can also access all form values at once via `form.state.values`. Since `useForm` is reactive in React, accessing `form.state.values` directly inside your component body means the component will re-render whenever *any* value in the form changes.
30+
31+
If you only need a specific value and want to avoid unnecessary re-renders, it's recommended to subscribe to just the slice of state you need using `useStore`:
32+
33+
```tsx
34+
import { useStore } from '@tanstack/react-form'
35+
36+
function AgeDisplay({ form }) {
37+
// Subscribes and re-renders only when 'age' changes
38+
const age = useStore(form.store, (state) => state.values.age)
39+
40+
return <div>Age: {age}</div>
41+
}
42+
```
43+
44+
### 3. Inside a `<form.Field>`
45+
When defining a field using the `<form.Field>` component, the current field value is provided directly via `field.state.value` inside the render prop.
46+
47+
```tsx
48+
<form.Field name="age">
49+
{(field) => (
50+
<div>
51+
Current age: {field.state.value}
52+
</div>
53+
)}
54+
</form.Field>
55+
```
56+
57+
## Setting Field Values
58+
59+
To update a field's value programmatically, use `form.setFieldValue(fieldName, newValue)`.
60+
61+
```tsx
62+
const form = useForm({
63+
defaultValues: {
64+
status: 'inactive',
65+
},
66+
})
67+
68+
const activate = () => {
69+
// Programmatically update the 'status' field
70+
form.setFieldValue('status', 'active')
71+
}
72+
73+
return (
74+
<button type="button" onClick={activate}>
75+
Set Status Active
76+
</button>
77+
)
78+
```
79+
80+
### Advanced Setting Options
81+
82+
The `setFieldValue` function optionally accepts an `options` object as its third argument. You can use this to control whether the field should be marked as "touched" or "dirty" when the value is updated.
83+
84+
```tsx
85+
form.setFieldValue('status', 'active', {
86+
dontUpdateMeta: true, // Prevents updating dirty/touched state
87+
})
88+
```
89+
90+
## Listening for Changes
91+
92+
If you need to automatically set one field's value exactly when another field changes, you should look into the [Listener API](./listeners.md). Listeners allow you to subscribe to `onChange` events safely without triggering infinite render loops.

0 commit comments

Comments
 (0)