Replies: 11 comments 4 replies
-
Can you provide a little more details of the problem you are facing? Why do you need to use setValue? You can set the value for config.test in defaultValue, like this |
Beta Was this translation helpful? Give feedback.
-
Thanks for the follow up.. The idea is that I make an API request which gives me object with 8 key values. and 2 of these keys have nested objects inside. Now, I want to show an edit form with these key values. so I use The problem is that i don't want to be using separate Isn't there any other way ? |
Beta Was this translation helpful? Give feedback.
-
Please post an example of how this object would look like including the nested objects. It would help me visualize a possible solution. |
Beta Was this translation helpful? Give feedback.
-
{
delay: 200,
data: {
amount: 15,
test: "30"
},
data2: {
amount: 40,
test: "35"
},
good: 123,
bad: 456
} Now, in my form, I have 7 input fields and as you can see above, we also have 7 . So each input contains each thing. Currently, As I am doing now,
I don't want to be doing so many doing
doesn't work. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
@dvdprr6 Thanks a lot for the nice explanation. I think we are almost there. Now as I said, defaultValues that I have to put come from REST API... which means |
Beta Was this translation helpful? Give feedback.
-
You would have to make the request in your parent component and then mount the form. The example below, the setConfig simulates yours API request and when you recieve the request you can use setOpen to mount the form function App() {
const [config, setConfig] = useState({});
const [open, setOpen] = useState(false);
useEffect(() => {
setConfig({
config: { delay: 200, data: { amount: 15, test: "30" } }
});
setOpen(true);
}, []);
return (
<div>{open ? <MyForm {...config} /> : <div></div>}</div>
)
}
const MyForm = (config) => {
console.log(config);
const { control, handleSubmit } = useForm({
defaultValues: { ...config }
});
const onSubmit = (form) => console.log(form);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<Controller
name="config.delay"
control={control}
render={({ field: { value, onChange } }) => (
<input value={value} onChange={(e) => onChange(e.target.value)} />
)}
/>
<Controller
name="config.data.amount"
control={control}
render={({ field: { value, onChange } }) => (
<input value={value} onChange={(e) => onChange(e.target.value)} />
)}
/>
<Controller
name="config.data.test"
control={control}
render={({ field: { value, onChange } }) => (
<input value={value} onChange={(e) => onChange(e.target.value)} />
)}
/>
<input type="submit" />
</form>
);
}; Assuming your API request it wrapped in a promise, perhaps your useEffect in your parent component would look something like this: useEffect(() => {
const fetchConfig = async () => await getConfig()
fetchConfig.then(() => {
setConfig({
config: { delay: 200, data: { amount: 15, test: "30" } }
});
setOpen(true)
})
},[]) |
Beta Was this translation helpful? Give feedback.
-
I re-looked at your setValue problem. Did you try to wrap it in the useEffect? Because it actually works by using the setValue in the useEffect. function App() {
const { control, handleSubmit, setValue } = useForm({});
useEffect(() => {
setValue("config", { delay: 200, data: { amount: 15, test: "30" } });
}, []);
const onSubmit = (form) => console.log(form);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<Controller
name="config.delay"
control={control}
render={({ field: { value, onChange } }) => (
<input value={value} onChange={(e) => onChange(e.target.value)} />
)}
/>
<Controller
name="config.data.amount"
control={control}
render={({ field: { value, onChange } }) => (
<input value={value} onChange={(e) => onChange(e.target.value)} />
)}
/>
<Controller
name="config.data.test"
control={control}
render={({ field: { value, onChange } }) => (
<input value={value} onChange={(e) => onChange(e.target.value)} />
)}
/>
<input type="submit" />
</form>
);
} |
Beta Was this translation helpful? Give feedback.
-
can you provide a codesandbox? |
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 Hey all, having a similar problem I think. I've put together a codesandbox: https://codesandbox.io/s/eloquent-banach-zho9o?file=/src/App.tsx If I try: If I try: But we don't know the id in advance, and will come from API. Please see TS error on Codesandbox (or attached screenshot). |
Beta Was this translation helpful? Give feedback.
-
Several years late, but you can put your multiple values in objects and set that value.
You'd still need to call
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
The problem is that inputField doesn't show the
config.test
value. This gets resolved ifI don't use
setValue('config', config);
and directly dosetValue('config.test', config.test);
but doing this is just horrible as I have so many variables and I don't wanna be callingsetValue
all the time.Beta Was this translation helpful? Give feedback.
All reactions