T
TanStack4y ago
ratty-blush

PUT request with controlled form

Hi, I don't know how to perform a PUT request with RQ, everything I found indicates use of React Hook Form, whereas the project I am on doesn't have it, how would I be able to insert the values into useState so that I can put them inside an input's value prop and update them with PUT
4 Replies
yappiest-sapphire
yappiest-sapphire4y ago
That's a quite generic question that doesn't give enough context to answer properly. The standard answer would be: react-query doesn't actually make the requests for you, so it doesn't care if it's put, post or get. You definitely want to look into mutations. You certainly don't have to use a form library, but react-query also doesn't manage the form state for you, as this is client state.
ratty-blush
ratty-blushOP4y ago
const { isLoading, data, onSuccess } = useUsersDetails(userId);

if (onSuccess) {
setformValue(data?.[0])
}

const [formValue, setformValue] = useState(data?.[0] || {})
const { isLoading, data, onSuccess } = useUsersDetails(userId);

if (onSuccess) {
setformValue(data?.[0])
}

const [formValue, setformValue] = useState(data?.[0] || {})
This is what I have so far, though each time the id changes now, where ever I inserted formValue.name instead of data[0].name it wouldn't refresh, so the first person you got, their name would remain even when you change the id and get a new user
yappiest-sapphire
yappiest-sapphire4y ago
ratty-blush
ratty-blushOP4y ago
Cool, I shall take a look and try and integrate it, thank you

Did you find this page helpful?