How to use useState with TextField? #6104
-
Hi, Which approach is better, A or B? A. const [text, setText] = useState('');
return (
<TextField>
<Label>Job post URL</Label>
<Input
value={text}
onChange={(e) => setText(e.target.value)}
/>
</TextField>
) B. const [text, setText] = useState('');
return (
<TextField value={text} onChange={setText}>
<Label>Job post URL < /Label>
<Input/>
</TextField>
) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You'll want to use B so that the value can propagate down to other subcomponents of TextField that might need to use it, like Under the hood, passing it into TextField means it uses the useTextField hook, while just passing it to Input is similar to just using a native |
Beta Was this translation helpful? Give feedback.
You'll want to use B so that the value can propagate down to other subcomponents of TextField that might need to use it, like
<FieldError />
for validation, and also receive accessibility features provided by useTextField.Under the hood, passing it into TextField means it uses the useTextField hook, while just passing it to Input is similar to just using a native
<input />
.